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