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