Libevent Multithread Support

user109343 picture user109343 · Feb 5, 2012 · Viewed 8.7k times · Source

i have a few questions regarding libevent2 and its multithread support.

Does libevent support multiple Threads? What i would like to achieve is something like this:

  1. Create an event_base in a single thread.
    • In this single Thread setup events and associate them to the event base. Also register Callbacks for each observed event.
  2. As soon as an observed event occurs, execute the registered callback in some other (worker)thread.

Is it possible to do s.th like this with libevent? Or are there any other approaches to support multiple cores?

Thank you very much

Answer

kainan picture kainan · May 14, 2013

If you add evthread_use_pthreads(); you must have -levent_pthreads

Example:

gcc chat.c -o chat -levent -lpthread -levent_pthreads

and:

$> ls /usr/lib/libevent*.a
/usr/lib/libevent.a  /usr/lib/libevent_core.a  /usr/lib/libevent_extra.a  /usr/lib/libevent_openssl.a  /usr/lib/libevent_pthreads.a