[systemd-devel] libsystemd: Should each thread open its own dbus connection (sd_bus*)?

Lennart Poettering lennart at poettering.net
Fri Nov 16 13:15:01 UTC 2018


On Fr, 16.11.18 08:02, Fabian Knapp (knapp at ambibox.de) wrote:

> I have an executable that opens a dbus connection with
> sd_bus_open_system at init time and cleanup and close this
> connection only on exit.
> 
> This connection is used in multiple threads (in a thread safe
> manner) and im wondering if this is suitable since sd_bus_add_match
> seems to time out if the sd_bus* object is created from another
> thread than the call to sd_bus_add_match happens.

sd-bus is not thread-safe. If you want to share connections between
threads you have to add a lock around it, which you need to allocate
yourself, and take whenever you access the bus and release it right
after.

I'd generally suggest using one connection per thread though, but this
has drawbacks (no global ordering and so on), so it really matters on
your usecase. But key really is: whatever you do, if you do use the
same bus object from multiple threads you need to lock around it,
sd-bus is not thread-safe on is own.

Lennart

-- 
Lennart Poettering, Red Hat


More information about the systemd-devel mailing list