GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

mainloop for thread synchronization

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
invictus



Joined: 11 Apr 2008
Posts: 4

PostPosted: Fri Apr 11, 2008 3:31 pm    Post subject: mainloop for thread synchronization Reply with quote

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?
Back to top
invictus



Joined: 11 Apr 2008
Posts: 4

PostPosted: Fri Apr 11, 2008 3:31 pm    Post subject: Reply with quote

For reference, the QT threading behavior is described here: http://doc.trolltech.com/4.0/threads.html
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 376
Location: State College, Pennsylvania

PostPosted: Fri Apr 11, 2008 4:57 pm    Post subject: Reply with quote

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().
Back to top
invictus



Joined: 11 Apr 2008
Posts: 4

PostPosted: Fri Apr 11, 2008 6:19 pm    Post subject: Reply with quote

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
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 376
Location: State College, Pennsylvania

PostPosted: Fri Apr 11, 2008 6:55 pm    Post subject: Reply with quote

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.
Back to top
invictus



Joined: 11 Apr 2008
Posts: 4

PostPosted: Sat Apr 12, 2008 10:07 am    Post subject: Reply with quote

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).
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP