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

Ted Gould ted at ubuntu.com
Fri Dec 2 16:28:02 UTC 2016


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-devel/attachments/20161202/8e51f0c7/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part
URL: <https://lists.ubuntu.com/archives/ubuntu-devel/attachments/20161202/8e51f0c7/attachment.pgp>


More information about the ubuntu-devel mailing list