multithreadinglibevent

Libevent Multithread Support


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


Solution

  • You would need some support for thread pooling. As of 2.0.x Libevent doesn't have one of these built in right now, but it might in the future.

    There have been a few proposed extensions you might want to look into. Mark Ellzey has a library called "libevthr" that he uses for thread pools in libevhtp. You can find it in the libevhtp repository. Mark Heily has a proposed patch to add an EV_PARALLEL flag to let Libevent use libpthread_workqueue. It appeared on the libevent-users mailing list here.

    If neither of those works out for you, you could do it yourself by picking any work-queue implementation that you like, and writing a Libevent callback to automatically queue your real callback for another thread.