Using GNotification in a multi-threaded GApplication

Dumb question - is it safe to call g_application_send_notification() from different threads? Looking at the source, I presume it is necessary to wrap it into a G_LOCK()/G_UNLOCK() (or similar) sequence to protect against races, but then it looks sane to me. Is that assumption correct?

And any action (e.g. by calling g_notification_add_button_with_target()) added in the thread is always transmitted to the main thread, but safe otherwise?

Thanks in advance for a clarification, Albrecht.

I don’t think so. You really need to make sure to use objects only on the thread they were created on, unless documented otherwise. The proper way to do this is to call g_idle_add() to run a callback on the main thread, and then call g_application_send_notification() in that callback. Fortunately, that’s easy.

What you’re overlooking is that even if you add locking, (a) other code using the object does not have locking, and (b) callbacks will likely be invoked on an unexpected GMainContext. For objects to be thread safe, that all needs to be handled in the object implementation.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.