#include #include #include #include #ifdef _WIN32 #else #include #endif // !_WIN32 using namespace std; static timeval t1 = {1,0}; void timer1(int sock,short which, void *arg){ cout << "[timer1]" << flush; event* ev = (event*)arg; if(!evtimer_pending(ev,&t1) ){ evtimer_del(ev); evtimer_add(ev,&t1); } } void timer2(int sock,short which, void *arg){ cout << "[timer2]" << flush; this_thread::sleep_for(3000ms); //c++ 11 } void timer3(int sock,short which, void *arg){ cout << "[timer3]" << flush; } int main(int agrc,char** agrv){ #ifdef _WIN32 //初始化socket库 WSADATA wsa; WSAStartup(MAKEWORD(2, 2), &wsa); #else if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) { //忽略管道信号,发送数据给已关闭的socket,会飞掉! return 1; } #endif event_base *base = event_base_new(); //定时器 cout << "test timer" << endl; //非持久定时器 只进入一次 event* evl = evtimer_new(base ,timer1,event_self_cbarg()); if(evl == NULL){ cout << "evtimer_new timer1 failed!" <