simple sever

Simon McVittie simon.mcvittie at collabora.co.uk
Fri Jan 10 10:59:56 PST 2014


On 09/01/14 13:48, Edgard Lima wrote:
> 1- In receiving (and reply) method thread
>     dbus_connection_read_write(_connection, 0);
>   dbus_msg = dbus_connection_pop_message(_connection);
> 
> 2- In broadcasting signal
>     dbus_message_new_signal
>     dbus_connection_send
> 
> Is that right, it it thread safe? (operating in same connection across
> threads)

It's *meant* to work, if you either call dbus_threads_init_default()
from main() before you start a second thread, or have libdbus 1.7.6 or
later. (Or both, of course.)

However, libdbus does not have a particularly helpful design for
thread-safety (mainly as a result of not mandating a particular
main-loop implementation), so I wouldn't recommend it for multi-threaded
applications. GDBus, in GLib, is better-designed for this.

If you run into problems with the approach you suggested, you could try
dispatching events from one thread to the other via some sort of
thread-safe event queue (use a good library rather than inventing your
own), and doing all your DBusConnection interaction in the main thread.
This probably requires use of some sort of "main loop" event
abstraction, like the ones in GLib, Qt, libevent etc.; but if you're
doing IPC and multi-threading, you should probably use one of those anyway.

> Is there a "hello world" for that?

With multi-threading? In my experience it looks something like this:

helwllor odSegmentation fault, core dumped

(Sorry, couldn't resist.)

Joking aside: in my experience, the more you can treat threads like
mini-processes, with minimal or no shared state between threads, the
more reliable it's likely to be.

Regards,
    S



More information about the dbus mailing list