-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEventLoopThread.cpp
More file actions
40 lines (35 loc) · 799 Bytes
/
EventLoopThread.cpp
File metadata and controls
40 lines (35 loc) · 799 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "EventLoopThread.h"
#include <functional>
#include <assert.h>
// A LoopThread has a mutex
// bind to Thread::
EventLoopThread::EventLoopThread(): loop_(NULL), exiting_(false), thread_(std::bind(&EventLoopThread::threadFunc, this),"EventLoopThread"), mutex_(), cond_(mutex_){
//nothing to do
}
EventLoopThread::~EventLoopThread(){
exiting_ = true;
if(loop_!=NULL){
loop_->quit();
thread_.join();
}
}
EventLoop* EventLoopThread::startLoop(){
assert(!thread_.started());
thread_.start();
{
MutexLockGuard lock(mutex_);
// wait until threadFunc is running in Thread.
while(loop_==NULL) cond_.wait();
}
return loop_;
}
void EventLoopThread::threadFunc(){
EventLoop loop;
{
MutexLockGuard lock(mutex_);
loop_=&loop;
cond_.notify();
}
loop.loop();
loop_=NULL;
}