---
title: "使用 libevent 实现定时器（timer）"
author: "soli"
date: "2013-07-12T18:46:15Z"
tags: []
categories: ["互联网"]
slug: "libevent-timer"
draft: false
lastmod: "2013-07-12T18:46:47Z"
id: 1905245
url: "https://soli.iteye.com/blog/1905245"
---

```c

#include <event.h>
#include <stdio.h>
#include <time.h>

static void
hello(int fd, short event, void *arg)
{
    printf("hello man\n");
}

int main(int argc, const char *argv[]) 
{
    struct event_base *base;
    struct timeval tm;
    struct event evt;

    evutil_timerclear(&tm);
    tm.tv_sec = 10;

    base = event_base_new();
    //evtimer_set(&evt, hello, NULL);
    event_set(&evt, -1, EV_PERSIST, hello, NULL);
    event_base_set(base, &evt);
    evtimer_add(&evt, &tm);
    event_base_loop(base, 0);
    return 0;
}

```


参考：http://yaojingguo.iteye.com/blog/826196

注意注释处的不同。