test4.cc revision 2a18e699
12a18e699SShuo Chen// copied from muduo/net/tests/TimerQueue_unittest.cc
22a18e699SShuo Chen
32a18e699SShuo Chen#include "Channel.h"
42a18e699SShuo Chen#include "EventLoop.h"
52a18e699SShuo Chen
62a18e699SShuo Chen#include <boost/bind.hpp>
72a18e699SShuo Chen
82a18e699SShuo Chen#include <stdio.h>
92a18e699SShuo Chen
102a18e699SShuo Chenint cnt = 0;
112a18e699SShuo Chenmuduo::EventLoop* g_loop;
122a18e699SShuo Chen
132a18e699SShuo Chenvoid printTid()
142a18e699SShuo Chen{
152a18e699SShuo Chen  printf("pid = %d, tid = %d\n", getpid(), muduo::CurrentThread::tid());
162a18e699SShuo Chen  printf("now %s\n", muduo::Timestamp::now().toString().c_str());
172a18e699SShuo Chen}
182a18e699SShuo Chen
192a18e699SShuo Chenvoid print(const char* msg)
202a18e699SShuo Chen{
212a18e699SShuo Chen  printf("msg %s %s\n", muduo::Timestamp::now().toString().c_str(), msg);
222a18e699SShuo Chen  if (++cnt == 20)
232a18e699SShuo Chen  {
242a18e699SShuo Chen    g_loop->quit();
252a18e699SShuo Chen  }
262a18e699SShuo Chen}
272a18e699SShuo Chen
282a18e699SShuo Chenint main()
292a18e699SShuo Chen{
302a18e699SShuo Chen  printTid();
312a18e699SShuo Chen  muduo::EventLoop loop;
322a18e699SShuo Chen  g_loop = &loop;
332a18e699SShuo Chen
342a18e699SShuo Chen  print("main");
352a18e699SShuo Chen  loop.runAfter(1, boost::bind(print, "once1"));
362a18e699SShuo Chen  loop.runAfter(1.5, boost::bind(print, "once1.5"));
372a18e699SShuo Chen  loop.runAfter(2.5, boost::bind(print, "once2.5"));
382a18e699SShuo Chen  loop.runAfter(3.5, boost::bind(print, "once3.5"));
392a18e699SShuo Chen  loop.runEvery(2, boost::bind(print, "every2"));
402a18e699SShuo Chen  loop.runEvery(3, boost::bind(print, "every3"));
412a18e699SShuo Chen
422a18e699SShuo Chen  loop.loop();
432a18e699SShuo Chen  print("main loop exits");
442a18e699SShuo Chen  sleep(1);
452a18e699SShuo Chen}
46