0

I'm testing libevent's timer functionality.

I set the timer with the following, which fires the timer every 3 seconds:

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());

In the timeout_cb, I want it to run a few times then exit the dispatch loop:

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);
    }
}

I'm trying to avoid a custom struct for the arg in the callback.

event_base_loopexit needs the base_event. Is it possible to get the base_event from the evutil_socket_t or the event* timer?

jww
  • 97,681
  • 90
  • 411
  • 885

1 Answers1

0

There are two ways to fetch the base.

event_get_base will retrieve the base:

event_base* base = event_get_base(timer);

event_get_assignment will also retrieve the base:

event_base* base = NULL;
event_get_assignment(timer, &base, NULL, NULL, NULL, NULL);
jww
  • 97,681
  • 90
  • 411
  • 885