From 066164e742c2926da2678363a57e650539641070 Mon Sep 17 00:00:00 2001 From: adminPyf <3043130461@qq.com> Date: Thu, 10 Jun 2021 01:37:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E6=97=B6=E5=99=A8=E4=BA=8B=E4=BB=B6de?= =?UTF-8?q?mo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test_timer/makefile | 2 + test_timer/test_timer.cpp | 80 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 test_timer/makefile create mode 100644 test_timer/test_timer.cpp diff --git a/test_timer/makefile b/test_timer/makefile new file mode 100644 index 0000000..5c90478 --- /dev/null +++ b/test_timer/makefile @@ -0,0 +1,2 @@ +test_timer:test_timer.cpp + g++ $^ -o $@ -levent \ No newline at end of file diff --git a/test_timer/test_timer.cpp b/test_timer/test_timer.cpp new file mode 100644 index 0000000..743d3d9 --- /dev/null +++ b/test_timer/test_timer.cpp @@ -0,0 +1,80 @@ +#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!" <