1f4e8e3d3SShuo Chen // copied from muduo/net/tests/TimerQueue_unittest.cc
2f4e8e3d3SShuo Chen 
3f4e8e3d3SShuo Chen #include "Channel.h"
4f4e8e3d3SShuo Chen #include "EventLoop.h"
5f4e8e3d3SShuo Chen 
6f4e8e3d3SShuo Chen #include <boost/bind.hpp>
7f4e8e3d3SShuo Chen 
8f4e8e3d3SShuo Chen #include <stdio.h>
9f4e8e3d3SShuo Chen 
10f4e8e3d3SShuo Chen int cnt = 0;
11f4e8e3d3SShuo Chen muduo::EventLoop* g_loop;
12f4e8e3d3SShuo Chen 
13f4e8e3d3SShuo Chen void printTid()
14f4e8e3d3SShuo Chen {
15f4e8e3d3SShuo Chen   printf("pid = %d, tid = %d\n", getpid(), muduo::CurrentThread::tid());
16f4e8e3d3SShuo Chen   printf("now %s\n", muduo::Timestamp::now().toString().c_str());
17f4e8e3d3SShuo Chen }
18f4e8e3d3SShuo Chen 
19f4e8e3d3SShuo Chen void print(const char* msg)
20f4e8e3d3SShuo Chen {
21f4e8e3d3SShuo Chen   printf("msg %s %s\n", muduo::Timestamp::now().toString().c_str(), msg);
22f4e8e3d3SShuo Chen   if (++cnt == 20)
23f4e8e3d3SShuo Chen   {
24f4e8e3d3SShuo Chen     g_loop->quit();
25f4e8e3d3SShuo Chen   }
26f4e8e3d3SShuo Chen }
27f4e8e3d3SShuo Chen 
28f4e8e3d3SShuo Chen+muduo::TimerId toCancel;
29f4e8e3d3SShuo Chen+void cancelSelf()
30f4e8e3d3SShuo Chen+{
31f4e8e3d3SShuo Chen+  print("cancelSelf()");
32f4e8e3d3SShuo Chen+  g_loop->cancel(toCancel);
33f4e8e3d3SShuo Chen+}
34f4e8e3d3SShuo Chen+
35f4e8e3d3SShuo Chen int main()
36f4e8e3d3SShuo Chen {
37f4e8e3d3SShuo Chen   printTid();
38f4e8e3d3SShuo Chen   muduo::EventLoop loop;
39f4e8e3d3SShuo Chen   g_loop = &loop;
40f4e8e3d3SShuo Chen 
41f4e8e3d3SShuo Chen   print("main");
42f4e8e3d3SShuo Chen   loop.runAfter(1, boost::bind(print, "once1"));
43f4e8e3d3SShuo Chen   loop.runAfter(1.5, boost::bind(print, "once1.5"));
44f4e8e3d3SShuo Chen   loop.runAfter(2.5, boost::bind(print, "once2.5"));
45f4e8e3d3SShuo Chen   loop.runAfter(3.5, boost::bind(print, "once3.5"));
46f4e8e3d3SShuo Chen-  loop.runEvery(2, boost::bind(print, "every2"));
47f4e8e3d3SShuo Chen+  muduo::TimerId t = loop.runEvery(2, boost::bind(print, "every2"));
48f4e8e3d3SShuo Chen   loop.runEvery(3, boost::bind(print, "every3"));
49f4e8e3d3SShuo Chen+  loop.runAfter(10, boost::bind(&muduo::EventLoop::cancel, &loop, t));
50f4e8e3d3SShuo Chen+  toCancel = loop.runEvery(5, cancelSelf);
51f4e8e3d3SShuo Chen 
52f4e8e3d3SShuo Chen   loop.loop();
53f4e8e3d3SShuo Chen   print("main loop exits");
54f4e8e3d3SShuo Chen   sleep(1);
55f4e8e3d3SShuo Chen }
56