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