Repository navigation
Expand file tree
/
Copy pathEventLoopThreadPool_unittest.cc
More file actions
67 lines (56 loc) · 1.55 KB
/
Copy pathEventLoopThreadPool_unittest.cc
File metadata and controls
67 lines (56 loc) · 1.55 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
60
61
62
63
64
65
66
#include <muduo/net/EventLoopThreadPool.h>
#include <muduo/net/EventLoop.h>
#include <muduo/base/Thread.h>
#include <stdio.h>
using namespace muduo;
using namespace muduo::net;
void print(EventLoop* p = NULL)
{
printf("main(): pid = %d, tid = %d, loop = %p\n",
getpid(), CurrentThread::tid(), p);
}
void init(EventLoop* p)
{
printf("init(): pid = %d, tid = %d, loop = %p\n",
getpid(), CurrentThread::tid(), p);
}
int main()
{
print();
EventLoop loop;
loop.runAfter(11, std::bind(&EventLoop::quit, &loop));
{
printf("Single thread %p:\n", &loop);
EventLoopThreadPool model(&loop);
model.setThreadNum(0);
model.start(init);
assert(model.getNextLoop() == &loop);
assert(model.getNextLoop() == &loop);
assert(model.getNextLoop() == &loop);
}
{
printf("Another thread:\n");
EventLoopThreadPool model(&loop);
model.setThreadNum(1);
model.start(init);
EventLoop* nextLoop = model.getNextLoop();
nextLoop->runAfter(2, std::bind(print, nextLoop));
assert(nextLoop != &loop);
assert(nextLoop == model.getNextLoop());
assert(nextLoop == model.getNextLoop());
::sleep(3);
}
{
printf("Three threads:\n");
EventLoopThreadPool model(&loop);
model.setThreadNum(3);
model.start(init);
EventLoop* nextLoop = model.getNextLoop();
nextLoop->runInLoop(std::bind(print, nextLoop));
assert(nextLoop != &loop);
assert(nextLoop != model.getNextLoop());
assert(nextLoop != model.getNextLoop());
assert(nextLoop == model.getNextLoop());
}
loop.loop();
}