环境 VS2017、ubuntu16.04
官网网址:http://www.linuxfromscratch.org/blfs/view/svn/basicnet/libevent.html 下载 安装:libevent-2.1.8-stable.tar.gz tar zxvf libevent-2.1.8-stable.tar.gz; ./configure; make ; make install
/sbin/ldconfig
mv libevent-2.1.8-stable/include /(工程目录中)/libevent //方便代码提示
配置:
配置:
#include <cstdio> #include <event.h> void on_time(int sock, short event, void *arg) { printf("hello world "); struct timeval tv; tv.tv_sec = 1; tv.tv_usec = 0; // 事件执行后,默认就被删除,所以需要重新添加 event_add((struct event*)arg, &tv); } int main() { // 初始化事件 event_init(); // 设置定时器回调函数 struct event ev_time; evtimer_set(&ev_time, on_time, &ev_time); //1s运行一次func函数 struct timeval tv; tv.tv_sec = 1; tv.tv_usec = 0; //添加到事件循环中 event_add(&ev_time, &tv); //程序等待就绪事件并执行事件处理 event_dispatch(); return 0; }
库依赖:-levent