Friday 2 December 2016

Re: cups-browsed uses GMainLoop and global variables, how to introduce locks against race conditions?

On Fri, 2016-12-02 at 12:58 -0200, Till Kamppeter wrote:
The solution would be to acquire a lock when starting to manipulate the   printer list and releasing the lock when done.    Now my qestion is, which functions I have to use for acquiring and   releasing locks when using GLib and GMainLoop? Probably it is not   correct to use the locks of pthread. Also it is probably best to use   Read/Write locks where only writing is exclusive but reading is allowed   to more than one thread at a time.  

Generally speaking the best way to do this is to use the mainloop itself as the lock. The mainloop is always on a single thread, so you should have the other threads put events into the main loop context and have them operate there on the data structure. So you'd, for instance, get all the information together on your Bonjour thread and when you're ready to add it put an action on the main loop with all that information.

The functions you're probably looking for are (as starting points):

g_main_context_get_thread_default()
g_idle_source_new()
g_source_attach()

Good luck!
Ted