140161064SShuo Chen// copied from muduo/net/tests/TimerQueue_unittest.cc
240161064SShuo Chen
340161064SShuo Chen#include "EventLoop.h"
440161064SShuo Chen
540161064SShuo Chen#include <boost/bind.hpp>
640161064SShuo Chen
740161064SShuo Chen#include <stdio.h>
840161064SShuo Chen
940161064SShuo Chenint cnt = 0;
1040161064SShuo Chenmuduo::EventLoop* g_loop;
1140161064SShuo Chen
1240161064SShuo Chenvoid printTid()
1340161064SShuo Chen{
1440161064SShuo Chen  printf("pid = %d, tid = %d\n", getpid(), muduo::CurrentThread::tid());
1540161064SShuo Chen  printf("now %s\n", muduo::Timestamp::now().toString().c_str());
1640161064SShuo Chen}
1740161064SShuo Chen
1840161064SShuo Chenvoid print(const char* msg)
1940161064SShuo Chen{
2040161064SShuo Chen  printf("msg %s %s\n", muduo::Timestamp::now().toString().c_str(), msg);
2140161064SShuo Chen  if (++cnt == 20)
2240161064SShuo Chen  {
2340161064SShuo Chen    g_loop->quit();
2440161064SShuo Chen  }
2540161064SShuo Chen}
2640161064SShuo Chen
27f4e8e3d3SShuo Chenmuduo::TimerId toCancel;
28f4e8e3d3SShuo Chenvoid cancelSelf()
29f4e8e3d3SShuo Chen{
30f4e8e3d3SShuo Chen  print("cancelSelf()");
31f4e8e3d3SShuo Chen  g_loop->cancel(toCancel);
32f4e8e3d3SShuo Chen}
33f4e8e3d3SShuo Chen
3440161064SShuo Chenint main()
3540161064SShuo Chen{
3640161064SShuo Chen  printTid();
3740161064SShuo Chen  muduo::EventLoop loop;
3840161064SShuo Chen  g_loop = &loop;
3940161064SShuo Chen
4040161064SShuo Chen  print("main");
4140161064SShuo Chen  loop.runAfter(1, boost::bind(print, "once1"));
4240161064SShuo Chen  loop.runAfter(1.5, boost::bind(print, "once1.5"));
4340161064SShuo Chen  loop.runAfter(2.5, boost::bind(print, "once2.5"));
4440161064SShuo Chen  loop.runAfter(3.5, boost::bind(print, "once3.5"));
45f4e8e3d3SShuo Chen  muduo::TimerId t = loop.runEvery(2, boost::bind(print, "every2"));
4640161064SShuo Chen  loop.runEvery(3, boost::bind(print, "every3"));
47f4e8e3d3SShuo Chen  loop.runAfter(10, boost::bind(&muduo::EventLoop::cancel, &loop, t));
48f4e8e3d3SShuo Chen  toCancel = loop.runEvery(5, cancelSelf);
4940161064SShuo Chen
5040161064SShuo Chen  loop.loop();
5140161064SShuo Chen  print("main loop exits");
5240161064SShuo Chen  sleep(1);
5340161064SShuo Chen}
54