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