tevent in multithreaded programs

Volker Lendecke Volker.Lendecke at SerNet.DE
Fri Jan 30 12:49:25 MST 2015

On Fri, Jan 30, 2015 at 12:30:27PM -0500, Jeff Layton wrote:
> Hi All!
> I'm looking at using tevent in a multithreaded program, primarily to
> handle socket descriptors. The basic idea is to have a tevent_wait loop
> running in one thread that will handle events by dispatching them to a
> workqueue (queue of jobs and a pile of threads to handle them).
> So basically we'll open a listener socket, and set it up to be watched
> by tevent and run that in one thread. When a connect request comes in,
> I want to dispatch the accept to a workqueue, which will then add the
> accept()ed fd to the event context.
> Questions:
> - is this legit? Can I add events in one thread while the event loop is
>   running in another?
> - if so, do I need to serialize that somehow (with pthread mutexes or
>   something)? I assume so, since tevent is based on talloc and you need
>   to synchronize between threads when dealing with talloc contexts...

You pretty much nailed it I guess. talloc does not play well
with threads if you want to share contexts. If you have a
talloc root per thread it should be okay, but the NULL
context is always a problem.

Who will handle the tevent_loop_once? If it's only one
thread, this might work. However, doing an tevent_add_fd
from a thread where another thread loops in
tevent_loop_once this will cause problems I guess. I'd
rather use other mechanisms to make the tevent_loop_once
thread add the fd.


SerNet GmbH, Bahnhofsallee 1b, 37081 Göttingen
phone: +49-551-370000-0, fax: +49-551-370000-9
AG Göttingen, HRB 2816, GF: Dr. Johannes Loxen
http://www.sernet.de, mailto:kontakt at sernet.de

More information about the samba-technical mailing list