test4.cc revision 9a1e991d
19a1e991dSShuo Chen// copied from muduo/net/tests/TimerQueue_unittest.cc 29a1e991dSShuo Chen 39a1e991dSShuo Chen#include "Channel.h" 49a1e991dSShuo Chen#include "EventLoop.h" 59a1e991dSShuo Chen 69a1e991dSShuo Chen#include <boost/bind.hpp> 79a1e991dSShuo Chen 89a1e991dSShuo Chen#include <stdio.h> 99a1e991dSShuo Chen 109a1e991dSShuo Chenint cnt = 0; 119a1e991dSShuo Chenmuduo::EventLoop* g_loop; 129a1e991dSShuo Chen 139a1e991dSShuo Chenvoid printTid() 149a1e991dSShuo Chen{ 159a1e991dSShuo Chen printf("pid = %d, tid = %d\n", getpid(), muduo::CurrentThread::tid()); 169a1e991dSShuo Chen printf("now %s\n", muduo::Timestamp::now().toString().c_str()); 179a1e991dSShuo Chen} 189a1e991dSShuo Chen 199a1e991dSShuo Chenvoid print(const char* msg) 209a1e991dSShuo Chen{ 219a1e991dSShuo Chen printf("msg %s %s\n", muduo::Timestamp::now().toString().c_str(), msg); 229a1e991dSShuo Chen if (++cnt == 20) 239a1e991dSShuo Chen { 249a1e991dSShuo Chen g_loop->quit(); 259a1e991dSShuo Chen } 269a1e991dSShuo Chen} 279a1e991dSShuo Chen 289a1e991dSShuo Chenint main() 299a1e991dSShuo Chen{ 309a1e991dSShuo Chen printTid(); 319a1e991dSShuo Chen muduo::EventLoop loop; 329a1e991dSShuo Chen g_loop = &loop; 339a1e991dSShuo Chen 349a1e991dSShuo Chen print("main"); 359a1e991dSShuo Chen loop.runAfter(1, boost::bind(print, "once1")); 369a1e991dSShuo Chen loop.runAfter(1.5, boost::bind(print, "once1.5")); 379a1e991dSShuo Chen loop.runAfter(2.5, boost::bind(print, "once2.5")); 389a1e991dSShuo Chen loop.runAfter(3.5, boost::bind(print, "once3.5")); 399a1e991dSShuo Chen loop.runEvery(2, boost::bind(print, "every2")); 409a1e991dSShuo Chen loop.runEvery(3, boost::bind(print, "every3")); 419a1e991dSShuo Chen 429a1e991dSShuo Chen loop.loop(); 439a1e991dSShuo Chen print("main loop exits"); 449a1e991dSShuo Chen sleep(1); 459a1e991dSShuo Chen} 46