 |
GTK+ Forums Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
|
|
|
| Author |
Message |
|
|
invictus
Joined: 11 Apr 2008 Posts: 4
|
Posted: Fri Apr 11, 2008 3:31 pm Post subject: mainloop for thread synchronization |
|
|
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
|
|
| Back to top |
|
 |
openldev Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 376 Location: State College, Pennsylvania
|
Posted: Fri Apr 11, 2008 4:57 pm Post subject: |
|
|
| 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
|
Posted: Fri Apr 11, 2008 6:19 pm Post subject: |
|
|
| 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
|
Posted: Fri Apr 11, 2008 6:55 pm Post subject: |
|
|
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
|
Posted: Sat Apr 12, 2008 10:07 am Post subject: |
|
|
| 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 |
|
 |
|
Powered by phpBB © 2001, 2005 phpBB Group CodeBB 1.0 Beta 2
|