19a1e991dSShuo Chen// copied from muduo/net/tests/TimerQueue_unittest.cc
29a1e991dSShuo Chen
39a1e991dSShuo Chen#include "EventLoop.h"
49a1e991dSShuo Chen
59a1e991dSShuo Chen#include <boost/bind.hpp>
69a1e991dSShuo Chen
79a1e991dSShuo Chen#include <stdio.h>
89a1e991dSShuo Chen
99a1e991dSShuo Chenint cnt = 0;
109a1e991dSShuo Chenmuduo::EventLoop* g_loop;
119a1e991dSShuo Chen
129a1e991dSShuo Chenvoid printTid()
139a1e991dSShuo Chen{
149a1e991dSShuo Chen  printf("pid = %d, tid = %d\n", getpid(), muduo::CurrentThread::tid());
159a1e991dSShuo Chen  printf("now %s\n", muduo::Timestamp::now().toString().c_str());
169a1e991dSShuo Chen}
179a1e991dSShuo Chen
189a1e991dSShuo Chenvoid print(const char* msg)
199a1e991dSShuo Chen{
209a1e991dSShuo Chen  printf("msg %s %s\n", muduo::Timestamp::now().toString().c_str(), msg);
219a1e991dSShuo Chen  if (++cnt == 20)
229a1e991dSShuo Chen  {
239a1e991dSShuo Chen    g_loop->quit();
249a1e991dSShuo Chen  }
259a1e991dSShuo Chen}
269a1e991dSShuo Chen
279a1e991dSShuo Chenint main()
289a1e991dSShuo Chen{
299a1e991dSShuo Chen  printTid();
309a1e991dSShuo Chen  muduo::EventLoop loop;
319a1e991dSShuo Chen  g_loop = &loop;
329a1e991dSShuo Chen
339a1e991dSShuo Chen  print("main");
349a1e991dSShuo Chen  loop.runAfter(1, boost::bind(print, "once1"));
359a1e991dSShuo Chen  loop.runAfter(1.5, boost::bind(print, "once1.5"));
369a1e991dSShuo Chen  loop.runAfter(2.5, boost::bind(print, "once2.5"));
379a1e991dSShuo Chen  loop.runAfter(3.5, boost::bind(print, "once3.5"));
389a1e991dSShuo Chen  loop.runEvery(2, boost::bind(print, "every2"));
399a1e991dSShuo Chen  loop.runEvery(3, boost::bind(print, "every3"));
409a1e991dSShuo Chen
419a1e991dSShuo Chen  loop.loop();
429a1e991dSShuo Chen  print("main loop exits");
439a1e991dSShuo Chen  sleep(1);
449a1e991dSShuo Chen}
45