165c497a3SShuo Chen// copied from muduo/net/tests/TimerQueue_unittest.cc
265c497a3SShuo Chen
365c497a3SShuo Chen#include "EventLoop.h"
465c497a3SShuo Chen
565c497a3SShuo Chen#include <boost/bind.hpp>
665c497a3SShuo Chen
765c497a3SShuo Chen#include <stdio.h>
865c497a3SShuo Chen
965c497a3SShuo Chenint cnt = 0;
1065c497a3SShuo Chenmuduo::EventLoop* g_loop;
1165c497a3SShuo Chen
1265c497a3SShuo Chenvoid printTid()
1365c497a3SShuo Chen{
1465c497a3SShuo Chen  printf("pid = %d, tid = %d\n", getpid(), muduo::CurrentThread::tid());
1565c497a3SShuo Chen  printf("now %s\n", muduo::Timestamp::now().toString().c_str());
1665c497a3SShuo Chen}
1765c497a3SShuo Chen
1865c497a3SShuo Chenvoid print(const char* msg)
1965c497a3SShuo Chen{
2065c497a3SShuo Chen  printf("msg %s %s\n", muduo::Timestamp::now().toString().c_str(), msg);
2165c497a3SShuo Chen  if (++cnt == 20)
2265c497a3SShuo Chen  {
2365c497a3SShuo Chen    g_loop->quit();
2465c497a3SShuo Chen  }
2565c497a3SShuo Chen}
2665c497a3SShuo Chen
2765c497a3SShuo Chenint main()
2865c497a3SShuo Chen{
2965c497a3SShuo Chen  printTid();
3065c497a3SShuo Chen  muduo::EventLoop loop;
3165c497a3SShuo Chen  g_loop = &loop;
3265c497a3SShuo Chen
3365c497a3SShuo Chen  print("main");
3465c497a3SShuo Chen  loop.runAfter(1, boost::bind(print, "once1"));
3565c497a3SShuo Chen  loop.runAfter(1.5, boost::bind(print, "once1.5"));
3665c497a3SShuo Chen  loop.runAfter(2.5, boost::bind(print, "once2.5"));
3765c497a3SShuo Chen  loop.runAfter(3.5, boost::bind(print, "once3.5"));
3865c497a3SShuo Chen  loop.runEvery(2, boost::bind(print, "every2"));
3965c497a3SShuo Chen  loop.runEvery(3, boost::bind(print, "every3"));
4065c497a3SShuo Chen
4165c497a3SShuo Chen  loop.loop();
4265c497a3SShuo Chen  print("main loop exits");
4365c497a3SShuo Chen  sleep(1);
4465c497a3SShuo Chen}
45