GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Apr 25, 2014 2:33 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: mainloop for thread synchronization
PostPosted: Fri Apr 11, 2008 3:31 pm 
Offline

Joined: Fri Apr 11, 2008 3:13 pm
Posts: 4
Hi

I am coming from a QT background, and are just learning the lower levels of gtk+ programming. One of the things I am wondering is whether I can make threading in gtk+ work the same way as it does in QT4.


Let me explain:

Let us assume I have a main thread running a main loop (like gtk_main() or the glib one), and then spawn another thread. Is it possible to run another independent main loop in this new thread at the same time as the main thread run its mainloop?

Now let us assume that this works. Is there any way I can do thread safe signaling between the threads? What I mean is that I want two threads that ONLY communicate with each other through signaling asynchronous events. For instance if the additional thread wants to update the GUI it will only do so by signaling the main thread and let the mainthread update it based on some information passed as an argument. As a result there will be no need for using the enter functions.

Any thoughts on this?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 3:31 pm 
Offline

Joined: Fri Apr 11, 2008 3:13 pm
Posts: 4
For reference, the QT threading behavior is described here: http://doc.trolltech.com/4.0/threads.html


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 4:57 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
Yes, you can run two separate gtk_main() loops in separate threads. However, you can only run GTK+ in one thread at a time. Therefore, you will need to use gdk_threads_enter()/leave().

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 6:19 pm 
Offline

Joined: Fri Apr 11, 2008 3:13 pm
Posts: 4
openldev wrote:
Yes, you can run two separate gtk_main() loops in separate threads. However, you can only run GTK+ in one thread at a time. Therefore, you will need to use gdk_threads_enter()/leave().


Ok let me try again. Instead of using the gtk_main() I use the glib main loop. No GTK+ involved. Is it possible to do what I tried to explain then? In that case, how?

Two threads, but with their own mainloops (glib ones) running independently of each other, communicating THREAD SAFE only through signaling eachother. E.g all communication between the thread is done through asynchronous calls pushing events to the other threads mainloop event queue. Each mainloop waits for events from the other thread (and of course other events attached to the independent mainloops).

Let me start at a more basic question: assuming I run a mainloop on one thread, is it possible to (thread safe) issue an event that execute some code on that thread from another thread without using gdk_threads_enter() ?

something like: g_send_event_on_other_mainloop(othermainloop, event) or something?




Btw, Foundations of GTK+ is a good book. Got it on the desk here :) Unfortunately its not that into the lowlevels of glib as I wish to discover here :P


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 11, 2008 6:55 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
Sorry, I didn't understand what you wanted answered in your original question. As long as GTK+ is out of the picture, you are good. This holds as long as you use GLib threads. From the GLib documentation (note the last sentence):

Quote:
You must call g_thread_init() before executing any other GLib functions in a threaded GLib program. After that, GLib is completely thread safe (all global data is automatically locked), but individual data structure instances are not automatically locked for performance reasons. So, for example you must coordinate accesses to the same GHashTable from multiple threads. The two notable exceptions from this rule are GMainLoop and GAsyncQueue, which are threadsafe and needs no further application-level locking to be accessed from multiple threads.


As for the book, I am in talks with Apress about possibly writing a second edition. Unfortunately with the original version, I wasn't able to cover _everything_ that I wanted to due to time constraints. I'm hoping to cover new features, and go more into the internals of GTK+/GLib and threading in the new edition.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 12, 2008 10:07 am 
Offline

Joined: Fri Apr 11, 2008 3:13 pm
Posts: 4
openldev wrote:
Sorry, I didn't understand what you wanted answered in your original question. As long as GTK+ is out of the picture, you are good. This holds as long as you use GLib threads. From the GLib documentation (note the last sentence):

Quote:
You must call g_thread_init() before executing any other GLib functions in a threaded GLib program. After that, GLib is completely thread safe (all global data is automatically locked), but individual data structure instances are not automatically locked for performance reasons. So, for example you must coordinate accesses to the same GHashTable from multiple threads. The two notable exceptions from this rule are GMainLoop and GAsyncQueue, which are threadsafe and needs no further application-level locking to be accessed from multiple threads.


Thanks...but I lets dive a bit further into the question:

Now lets assume I have 2 threads where each have their own mainloop. How can I signal the other threads mainloop so that some callback is executed on the other thread?

E.g. thread 1 and thread 2 have mainloop 1 and mainloop 2. thread 2 signals mainloop 1 and a callback is then executed on thread 1 (NOT on thread 2).


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group