-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathReactor_test04.cc
More file actions
70 lines (60 loc) · 1.54 KB
/
Copy pathReactor_test04.cc
File metadata and controls
70 lines (60 loc) · 1.54 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
67
68
69
70
#include <muduo/net/EventLoop.h>
//#include <muduo/net/EventLoopThread.h>
#include <muduo/base/Thread.h>
#include <boost/bind.hpp>
#include <stdio.h>
#include <unistd.h>
using namespace muduo;
using namespace muduo::net;
int cnt = 0;
EventLoop* g_loop;
void printTid()
{
printf("pid = %d, tid = %d\n", getpid(), CurrentThread::tid());
printf("now %s\n", Timestamp::now().toString().c_str());
}
void print(const char* msg)
{
printf("msg %s %s\n", Timestamp::now().toString().c_str(), msg);
if (++cnt == 20)
{
g_loop->quit();
}
}
void cancel(TimerId timer)
{
g_loop->cancel(timer);
printf("cancelled at %s\n", Timestamp::now().toString().c_str());
}
int main()
{
printTid();
sleep(1);
{
EventLoop loop;
g_loop = &loop;
print("main");
loop.runAfter(1, boost::bind(print, "once1"));
loop.runAfter(1.5, boost::bind(print, "once1.5"));
loop.runAfter(2.5, boost::bind(print, "once2.5"));
loop.runAfter(3.5, boost::bind(print, "once3.5"));
TimerId t45 = loop.runAfter(4.5, boost::bind(print, "once4.5"));
loop.runAfter(4.2, boost::bind(cancel, t45));
loop.runAfter(4.8, boost::bind(cancel, t45));
loop.runEvery(2, boost::bind(print, "every2"));
TimerId t3 = loop.runEvery(3, boost::bind(print, "every3"));
loop.runAfter(9.001, boost::bind(cancel, t3));
loop.loop();
print("main loop exits");
}
/*
sleep(1);
{
EventLoopThread loopThread;
EventLoop* loop = loopThread.startLoop();
loop->runAfter(2, printTid);
sleep(3);
print("thread loop exits");
}
*/
}