Using d-bus from multiple threads?

Burton Samograd burton at
Mon Aug 23 15:34:24 PDT 2010

Ivan Barr <itb.wrk at> writes:

> Hi Burton,
> Thank you very much for replying.
> Could you perhaps give an outline of your approach and anything you
> did specifically to get it to work across the threads? For example did
> you use multiple private connections or one? Did you use a standard
> MainLoop with the default GMainContext? Did you limit the use of
> proxies to specific threads etc.  etc.

First off, I did not used the glib bindings, but the standard C api
described in:

Other than requiring a lot boilerplate code for some basic things, it
worked out fine for me.

The program I wrote had a single main thread that was performing the
call to dbus_connection_read_write_dispatch.  There was only a single
connection which was shared between all threads.  I main thread
performed all the message processing and dispatching; occasionally a
worker thread would call a dbus method to another process.  I didn't
find a need for any sychronization around the connection object; I think
this is handled by the dbus library.

But, like I said in the original message, I had a problem with
cancelling a thread.  If the thread happened to get cancelled while in
the middle of a dbus call it seemed a lock could be held, which caused
the system to grind to a halt. This was the only major bug that I had
with multithreading and dbus (so far).

Mixing calls between threads seemed to work out fine, but maybe I just
got lucky.  It seems there's some thread synchronization already present
in dbus, but like I also said, it seemed a bit flakey and possibly not
all that tested.

I think the best way might be to handle all dbus calls from within a
single main thread and then having the worker threads dispatch to the
main thread when a dbus call is needed.  Basically I just went for it
(it was both my first dbus and multithreaded program) and everything
seemed to work pretty well.  Your mileage might vary...

Hope some of this might be helpful.

Burton Samograd

More information about the dbus mailing list