libevent 定时器

<span style="font-size:18px;">#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>

#include<event.h>



struct event ev;
struct timeval tv;

void time_cb(int fd,short event,void *argc)
{
        printf("timer wakeup\n");
        event_add(&ev,&tv);
}


int  main()
{
        struct event_base *base =(struct event_base*)event_init();

        tv.tv_sec = 10;
        tv.tv_usec = 0;
        evtimer_set(&ev,time_cb,NULL);
        event_add(&ev,&tv);
        event_base_dispatch(base);
        return 0;
}
</span>


编译:g++ -g test.cpp -o test -levent


如果出现错误如下:

./test: error while loading shared libraries: libevent-1.2.so.1: cannot open shared object file: No such file or directory


解决方案:

先执行命令:ldconfig

然后执行:./test


参考:http://blog.csdn.net/sahusoft/article/details/7388617



你可能感兴趣的:(libevent)