-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventLoopThread.cpp
More file actions
59 lines (54 loc) · 1.3 KB
/
EventLoopThread.cpp
File metadata and controls
59 lines (54 loc) · 1.3 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "EventLoopThread.h"
#include "EventLoop.h"
#include <boost/bind.hpp>
EventLoopThread::EventLoopThread(const ThreadInitCallback& cb)
: m_ploop(NULL),
m_bexiting(false),
m_thread(boost::bind(&EventLoopThread::threadFunc, this)),
m_mutex(),
m_cond(m_mutex),
m_callback(cb)
{
}
EventLoopThread::~EventLoopThread()
{
m_bexiting = true;
m_ploop->quit();
printf("EventLoopThread ~~~\n");
m_thread.join();
}
//run in old thread
EventLoop* EventLoopThread::startLoop()
{
assert(!m_thread.started());
m_thread.start();
printf("EventLoopThread::startLoop after start\n");
{
MutexLockGuard lock(m_mutex);
printf("EventLoopThread::startLoop after start 111\n");
while(m_ploop == NULL)
{
printf("EventLoopThread::startLoop after start 222\n");
m_cond.wait();
printf("EventLoopThread::startLoop after start 333\n");
}
}
return m_ploop;
}
//run in new thread
void EventLoopThread::threadFunc()
{
EventLoop loop;
if(m_callback)
{
m_callback(&loop);
}
{
MutexLockGuard lock(m_mutex);
m_ploop = &loop;
m_cond.notify();
}
printf("EventLoopThread::threadFunc 111111\n");
loop.loop();
printf("EventLoopThread::threadFunc 222222\n");
}