Search code examples
linuxlibevent

adding timer events with event_base_loop


my ebase thread perform the following loop constexpr int kFlag = EVLOOP_ONCE; while ((res = event_base_loop(ebase_, kFlag)) == 1) { yield(); }

and the other (main) thread adds periodic timer event. It adds the event after the ebase thread calls event_base_loop.

event* ev = event_new(base(), -1, EV_PERSIST | EV_TIMEOUT, &PeriodicCb, data);
CHECK(0 == event_add(ev, &tv));

However, I see that this timer event does not run. If I change kFlag from EVLOOP_ONCE to 0 it does run well. What am I missing?


Solution

  • The culprit is in the event base loop. The right code is like this:

    while ((res = event_base_loop(ebase, EVLOOP_ONCE)) >= 0) {
      if (start_cancel)
        break;
    }
    

    i.e. it should continue rolling with return value no less than 0 and not just 1.