环境 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   //方便代码提示

 

配置:

libevent环境搭建

配置:

libevent环境搭建

 

 

#include <cstdio>
#include <event.h>  

void on_time(int sock, short event, void *arg)
{
    printf("hello world\n");

    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

 

相关文章:

  • 2021-08-01
  • 2022-12-23
  • 2022-12-23
  • 2021-09-27
  • 2022-02-18
  • 2021-12-05
  • 2021-08-19
  • 2021-07-05
猜你喜欢
  • 2022-12-23
  • 2021-06-25
  • 2021-10-08
  • 2021-04-14
  • 2021-10-11
  • 2021-11-12
  • 2022-12-23
相关资源
相似解决方案