4

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

user109343
  • 43
  • 1
  • 1
  • 5
  • I'm thinking of doing something similar (http://stackoverflow.com/questions/21677154/libevent-multithreading-to-handle-http-keep-alive-connections) so I was wondering how you got on? Did it work out? Were there any pitfalls? Any feedback would be most welcome! – SlappyTheFish Apr 02 '14 at 09:04

2 Answers2

11

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
rid
  • 61,078
  • 31
  • 152
  • 193
kainan
  • 175
  • 2
  • 4
7

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.

nickm
  • 486
  • 2
  • 2
  • 2
    Hey Nick, if i add evthread_use_pthreads(); to my code i receive the error: undefined reference to `evthread_use_pthreads' To compile i use the following command: "gcc chat.c -o chat -levent -lpthread" and i suppose, that i need to link something else but i dont know what. Could you help me here? Thanks. – Filipe Santos Mar 12 '12 at 17:00