从计时器中检索基地?

时间:2014-02-25 00:49:28

标签: timer libevent

我正在测试libevent的计时器功能。

我使用以下命令设置计时器,每3秒触发一次计时器:

shared_ptr<event_base> base(event_base_new(), std::ptr_fun(event_base_free));
vector<shared_ptr<event> > std_events;
...

static const timeval three_sec = { 3, 0 };
event* tev = NULL;

tev = evtimer_new(base.get(), NULL, NULL);
rc = event_assign(tev, base.get(), -1, EV_PERSIST, timeout_cb, tev);

rc = event_add(tev, &three_sec);
shared_ptr<event> ptr(tev, std::ptr_fun(event_free));
std_events.push_back(ptr);

rc = event_base_dispatch(base.get());

timeout_cb中,我希望它运行几次然后退出调度循环:

void
timeout_cb(evutil_socket_t fd, short events, void *arg)
{
    static int count = 0;
    int rc = 0;

    event* timer = (event *)arg;
    event_base* base = NULL;      

    if(count++ == 5) {
        struct timeval delay = { 1, 0 };
        rc = event_base_loopexit(base, &delay);
    }
}

我正在尝试避免回调中arg的自定义结构。

event_base_loopexit需要base_event。是否可以从base_eventevutil_socket_t event*获取timer

1 个答案:

答案 0 :(得分:0)

有两种方法可以获取base

event_get_base将检索基数:

event_base* base = event_get_base(timer);

event_get_assignment还会检索base

event_base* base = NULL;
event_get_assignment(timer, &base, NULL, NULL, NULL, NULL);