-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventLoopThreadPool.cpp
More file actions
51 lines (45 loc) · 1.01 KB
/
EventLoopThreadPool.cpp
File metadata and controls
51 lines (45 loc) · 1.01 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
#include "EventLoopThreadPool.h"
#include "EventLoop.h"
#include "EventLoopThread.h"
#include <boost/bind.hpp>
EventLoopThreadPool::EventLoopThreadPool(EventLoop* baseloop)
: m_baseLoop(baseloop),
m_started(false),
m_threadsNum(0),
m_next(0)
{
}
EventLoopThreadPool::~EventLoopThreadPool()
{
}
void EventLoopThreadPool::start(const ThreadInitCallback& cb)
{
assert(!m_started);
m_baseLoop->assertInLoopThread();
m_started = true;
for(int i = 0; i < m_threadsNum; ++i)
{
EventLoopThread* t = new EventLoopThread(cb);
m_threads.push_back(t);
m_loops.push_back(t->startLoop());
}
if(m_threadsNum == 0 && cb)
{
cb(m_baseLoop);
}
}
EventLoop* EventLoopThreadPool::getNextLoop()
{
m_baseLoop->assertInLoopThread();
EventLoop* loop = m_baseLoop;
if(!m_loops.empty())
{
loop = m_loops[m_next];
++m_next;
if((size_t)m_next >= m_loops.size())
{
m_next = 0;
}
}
return loop;
}