#include <event2/event.h>
#include <event2/event_struct.h>
#include <event2/util.h>
#include <iostream>
void onTime(int sock, short event, void*arg)
{
std::cout << "On Timer" << std::endl;
}
int main()
{
#ifdef _WIN32//Win32必须初始化网络库
WORD wVersionRequested;
WSADATA wsaData;
wVersionRequested = MAKEWORD(2, 2);
(void)WSAStartup(wVersionRequested, &wsaData);
#endif
event_base *pBase = event_base_new();
struct event evTime;
event_assign(&evTime, pBase,-1, EV_PERSIST, onTime, NULL);
//evtimer_assign(&evTime, pBase, onTime, NULL);//和event_assign的区别是:该事件初始化的时候,事件不是EV_PERSIST,即该事件只执行一次,就会从事件队列中删除。
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
event_add(&evTime, &tv);
event_base_dispatch(pBase);
return 0;
}
Timer例子
最新推荐文章于 2023-11-30 22:18:10 发布