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