libevent定时器示例

1.源码部分

#include "event2/event-config.h"

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include "event2/event.h"
#include "event2/event_compat.h"
#include "event2/event_struct.h"
#include "util-internal.h"

int called = 0;  //被调用次数

#define NEVENT  20000  //事件数量

struct event *ev[NEVENT];  //事件结构体

struct evutil_weakrand_state weakrand_state;

//产生一个0到n-1的随机数
static int rand_int(int n)
{
    return evutil_weakrand_(&weakrand_state) % n;
}

//被调用的函数
static void time_cb(evutil_socket_t fd, short event, void *arg)
{
    struct timeval tv;

    called++;

    printf("called=[%d]\n", called);
}

int main(int argc, char **argv)
{
    struct timeval tv;
    int i;

    evutil_weakrand_seed_(&weakrand_state, 0);

    event_init();  //事件初始化

    for(i = 0; i

2.编译源码

$ gcc -o test-time test-time.c -L/home/autocnf/local/prior/lib -levent

3.测试运行

$ ./test-time

4.运行结果

called=[1]
called=[2]
called=[3]
called=[4]
called=[5]
called=[6]
called=[7]
called=[8]
called=[9]
......

你可能感兴趣的:(libevent定时器示例)