thread-safety

Havoc Pennington hp at redhat.com
Wed Dec 14 21:10:54 PST 2005


On Tue, 2005-12-13 at 16:33 -0500, Jeffrey Stedfast wrote:
> I'm about to dive into gnome-vfs and make it use the new HAL
> mount/unmount/eject fu that Kay has just added in 0.5.6...
> 
> What I need to know is whether doing things like sending dbus messages
> from multiple threads on the same bus, etc is going to cause problems
> (since gnome-vfs is mutlithreaded).
> 

libdbus is theoretically thread safe, it supports using the same
DBusConnection from multiple threads. You have to call
dbus_g_thread_init() to get it going in a glib app. All DBusConnection
methods will automatically do locking etc.

DBusMessage does not have any locks though; if you want to share a
message between threads, you need to do your own locking. Also once you
send the message, it will be owned by the DBusConnection thread so your
other threads need to stay away from it. There's no typical reason why
you'd share a message between threads or even keep a reference to a
message though, normally you would send it and forget it.

I would say libdbus probably has a thread-related bug or three, but
probably nothing fundamental. Judging by list traffic, some people have
had success using it with threads, and there's a basic test app in the
tree that works fine.

The GLib bindings are maybe more likely to have issues, but are probably
easy to fix if so.

You do have to be sure your threads don't step on each other; i.e. you
don't want one thread to steal another thread's messages or monopolize
the connection lock for a really long time or something. But that's just
a matter of thinking about this as you code.

Havoc




More information about the dbus mailing list