-
Notifications
You must be signed in to change notification settings - Fork 0
/
test4.cc
53 lines (43 loc) · 1.14 KB
/
test4.cc
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
// copied from muduo/net/tests/TimerQueue_unittest.cc
#include "EventLoop.h"
#include <boost/bind.hpp>
#include <stdio.h>
int cnt = 0;
muduo::EventLoop* g_loop;
void printTid()
{
printf("pid = %d, tid = %d\n", getpid(), muduo::CurrentThread::tid());
printf("now %s\n", muduo::Timestamp::now().toString().c_str());
}
void print(const char* msg)
{
printf("msg %s %s\n", muduo::Timestamp::now().toString().c_str(), msg);
if (++cnt == 20)
{
g_loop->quit();
}
}
muduo::TimerId toCancel;
void cancelSelf()
{
print("cancelSelf()");
g_loop->cancel(toCancel);
}
int main()
{
printTid();
muduo::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"));
muduo::TimerId t = loop.runEvery(2, boost::bind(print, "every2"));
loop.runEvery(3, boost::bind(print, "every3"));
loop.runAfter(10, boost::bind(&muduo::EventLoop::cancel, &loop, t));
toCancel = loop.runEvery(5, cancelSelf);
loop.loop();
print("main loop exits");
sleep(1);
}