从计时器检索基



我正在测试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.是否可以从evutil_socket_tevent* timer获得base_event

有两种方法可以获取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);

相关内容

  • 没有找到相关文章

最新更新