Can I call event_base_loopbreak
to int event_base_dispatch(struct event_base *base)
that run in another thread?

- 41,402
- 9
- 98
- 136

- 53
- 2
- 5
-
Adding about 2~3 lines of context makes it quite a bit more probable that people can answer your question - this is **really** concise. – Niels Keurentjes Sep 16 '14 at 07:54
3 Answers
It's possible to stop event_base_dispatch
with event_base_loopbreak
from another thread. Just enable multithreading support by calling evthread_use_pthreads
or evthread_use_windows_threads
before event_base
created.

- 41
- 5
-
1I had this problem where event_base_loop used with EVLOOP_NO_EXIT_ON_EMPTY in another thread, is not executing events created in main thread. Once I put the evthread_use_pthreads before event_base creation phew.. it worked. Very thanks mate – King Jun 18 '20 at 05:57
Calling event_base_loopbreak from outside of the event loop doesn't actually have an effect and inside the loop, event_base_loopbreak does not break from the loop. So, calling event_base_loopbreak to int event_base_dispatch(struct event_base *base) that run in another thread, should not have any effect.

- 6,980
- 4
- 30
- 69
-
2So, how can I stop event_base_dispatch from another thread? OR can I add event to event_base while event_base_dispatch is working? – user3315755 Sep 16 '14 at 07:52
I'm also meet the same issue, no matter calling event_base_loopbreak() or event_add() from other thread are not working.
Ref to How to break out libevent's dispatch loop, I had to set a timer event before start the event loop, and polling the exit flag to call event_base_loopbreak() from the event's callback, that can exit the event loop successfully