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