Re: Gtk+ and multithreating
Am 2007-05-16 21:32:39, schrieb Chris Vine: The documentation is here: http://developer.gnome.org/doc/API/2.0/glib/glib-The-Main-Event-Loop.html#g-idle-add I have the full documentation installed and I have a hardcopy of the PDF. The idea is that you call g_idle_add() in your worker thread with a callback (function pointer and data) which you want to execute in the main program (GUI) loop. This is what I have tried yesterday... A simple example would be to call: g_idle_add((GSourceFunc)g_print, Hello\n); If you call this in the worker thread, it will print Hello in the GTK+ main program thread (you must have called g_thread_init(NULL) first). See other message: I have a problem with g_thread_init(NULL) and do not know why. Incidentally, instead of using pthreads, you can use the platform independent GThread implementation. See: http://developer.gnome.org/doc/API/2.0/glib/glib-Threads.html I was trying already... (checking all posibilities) Threading is realy new for me.. Thanks, Greetings and nice Day Michelle Konzack Systemadministrator Tamay Dogan Network Debian GNU/Linux Consultant -- Linux-User #280138 with the Linux Counter, http://counter.li.org/ # Debian GNU/Linux Consultant # Michelle Konzack Apt. 917 ICQ #328449886 50, rue de Soultz MSN LinuxMichi 0033/6/6192519367100 Strasbourg/France IRC #Debian (irc.icq.com) ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: Gtk+ and multithreating
I'm doing a multithreading program using GTK+ (a POS emulator) and I'm using something like that: // #include gtk/gtk.h gpointer cb_t1(gpointer d) { while (1) { g_usleep(1000 * 2000); gdk_threads_enter(); /* my turn! i'll use GTK+... */ g_debug(thread 1 in action!); gtk_label_set_text(GTK_LABEL(d), thread1); gdk_flush(); /* flushes all pending commands... */ gdk_threads_leave(); /* i'm done! */ } } gpointer cb_t2(gpointer d) { while (1) { g_usleep(1000 * 3000); gdk_threads_enter(); /* my turn! i'll use GTK+... */ g_debug(thread 2 in action!); gtk_label_set_text(GTK_LABEL(d), thread2); gdk_flush(); /* flushes all pending commands... */ gdk_threads_leave(); /* i'm done! */ } } gboolean cb_key_press (GtkWidget *w, GdkEventKey *e, gpointer d) { g_message(%d, e-keyval); } int main(int argc, char *argv[]) { GtkWidget *window; GtkWidget *label; g_thread_init(NULL); /* glib threads init */ gdk_threads_init(); /* gdk threads init */ gdk_threads_enter(); /* protects the main loop */ gtk_init(argc, argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_widget_set_events(window, GDK_KEY_PRESS_MASK); g_signal_connect( G_OBJECT(window), destroy, G_CALLBACK(gtk_main_quit), NULL); g_signal_connect( G_OBJECT(window), key-press-event, G_CALLBACK(cb_key_press), NULL); gtk_widget_show(window); label = gtk_label_new(); gtk_container_add(GTK_CONTAINER(window), label); gtk_widget_show(label); g_thread_create(cb_t1, (gpointer) label, FALSE, NULL); g_thread_create(cb_t2, (gpointer) label, FALSE, NULL); gtk_main(); gdk_threads_leave(); /* we're done... */ } // Here we have three threads unsing the GTK+ without conflicts... Basically, we must protect the gtk_main() loop with the gdk_threads_enter()/gdk_threads_leave() pair and for each function that uses the GTK+ we must surrounding that with a gtk_thread_enter()/gdk_threads_leave() pair. This functions just lock a previously definied mutex (or something like that). More info: http://developer.gnome.org/doc/API/2.0/gdk/gdk-Threads.html http://developer.gnome.org/doc/API/2.0/glib/glib-Threads.html PS: Sorry Michael. =P ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: Gtk+ and multithreating
Am 2007-05-14 20:56:05, schrieb Cédric Lucantis: Michael is probably right, but to answer your question there's a good documentation about that in the GLib manual. See the sections 'Threads', 'Thread Pools' and 'Asynchronous Queues' under 'Glib Core Application Support'. If you want to know more, there's a tutorial here (about pthread but with a general introduction) : http://www.llnl.gov/computing/tutorials/pthreads Thursday evening downloaded with some other pthread-Tutorials and 3 hours later tried... I have put my function into pthread_create() like in the example of those tutorials and it was just working... (generaly) But now I must find out, how to put the received data (from the thread) back into the GtkTreeView widget (updating).. Thanks, Greetings and nice Day Michelle Konzack Systemadministrator Tamay Dogan Network Debian GNU/Linux Consultant -- Linux-User #280138 with the Linux Counter, http://counter.li.org/ # Debian GNU/Linux Consultant # Michelle Konzack Apt. 917 ICQ #328449886 50, rue de Soultz MSN LinuxMichi 0033/6/6192519367100 Strasbourg/France IRC #Debian (irc.icq.com) ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: Gtk+ and multithreating
On Wed, 2007-05-16 at 21:32 +0100, Chris Vine wrote: On Wed, 2007-05-16 at 16:44 +0200, Michelle Konzack wrote: Am 2007-05-14 22:22:34, schrieb Chris Vine: Pass the callbacks representing the events concerned to the main program loop using g_idle_add(). Make sure the callbacks return FALSE. g_idle_add() is thread safe, provided that you have initialised glib with g_thread_init(). Chris - END OF REPLIED MESSAGE - Forgive me if I do not understand this... Do you have a short exanple? For the phtreads I have only copied an example-sniplet and modified a bit and it was just working... :-) The documentation is here: http://developer.gnome.org/doc/API/2.0/glib/glib-The-Main-Event-Loop.html#g-idle-add The idea is that you call g_idle_add() in your worker thread with a callback (function pointer and data) which you want to execute in the main program (GUI) loop. http://developer.gnome.org/doc/API/2.0/glib/glib-Threads.html A simple example would be to call: g_idle_add((GSourceFunc)g_print, Hello\n); Actually it occurs to me that this may not work without a wrapper function which returns FALSE. I am not sure what the effect of casting a function with a void return type to a GSourceFunc type (which returns a gboolean/int type) is. Anyway, just put it in a wrapper function to try out the example. Chris ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: Gtk+ and multithreating
On Wed, 2007-05-16 at 21:38 +0100, Chris Vine wrote: On Wed, 2007-05-16 at 21:32 +0100, Chris Vine wrote: On Wed, 2007-05-16 at 16:44 +0200, Michelle Konzack wrote: Am 2007-05-14 22:22:34, schrieb Chris Vine: Pass the callbacks representing the events concerned to the main program loop using g_idle_add(). Make sure the callbacks return FALSE. g_idle_add() is thread safe, provided that you have initialised glib with g_thread_init(). Chris - END OF REPLIED MESSAGE - Forgive me if I do not understand this... Do you have a short exanple? For the phtreads I have only copied an example-sniplet and modified a bit and it was just working... :-) The documentation is here: http://developer.gnome.org/doc/API/2.0/glib/glib-The-Main-Event-Loop.html#g-idle-add The idea is that you call g_idle_add() in your worker thread with a callback (function pointer and data) which you want to execute in the main program (GUI) loop. http://developer.gnome.org/doc/API/2.0/glib/glib-Threads.html A simple example would be to call: g_idle_add((GSourceFunc)g_print, Hello\n); Actually it occurs to me that this may not work without a wrapper function which returns FALSE. I am not sure what the effect of casting a function with a void return type to a GSourceFunc type (which returns a gboolean/int type) is. Anyway, just put it in a wrapper function to try out the example. And you would need to put the Hello argument on the heap, and free it in the wrapper function, because otherwise it will go out of scope before the idle handler is called. (Sigh). Use g_strdup() to create the string on the heap. Simple examples are never that simple. Chris ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: Gtk+ and multithreating
On Wed, 2007-05-16 at 23:22 +0100, Chris Vine wrote: And you would need to put the Hello argument on the heap, and free it in the wrapper function, because otherwise it will go out of scope before the idle handler is called. (Sigh). Use g_strdup() to create the string on the heap. Simple examples are never that simple. On this case, Hello is a statically allocated string. It's always available and never is destroyed (it is always on the heap, as it were). Not something that's typically useful, though. In general, yes, data must be allocated on the heap, not the stack, in order to be passed to a callback handler like this. Unless it's a global static object (such as a string literal). Chris ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
AW: Gtk+ and multithreating
Hi list,... ...im sorry if i missunderstand something... Am 14.05.2007 19:53:44 schrieb(en) Michelle Konzack: Hello *, I am new on this list, but have already codes some VERY simple admin GUI's... Now I have a bigger problem/Application which need definitiv multithreading since it must poll several servers and update several TreeView and ListView. I have no experience with it, since I am one of those console-singel-task-no-one-bother-me programmers (Debian GNU/Linux)... :-) OK, for now my GUI is running and then I use a loop like 8 window_main=create_window_main(); while ( TRUE ) { while ( gtk_events_pending() ) gtk_main_iteration(); some_code_to_update_the_dialog } 8 ...could it be two threads try updating the same GUI-Structure !? - trying to resolve their reflections of their updates on the Object-Tree or something that way?? Maybe the simplest way is to code the main thread first, and (ok, here im in pain) then, following new threads, lock that functions changing something GUI-pending for thread-safety; i had already such a Prog, but cannot remember. Was that something with g_mutex_lock !? Threre was a tutorial...anywhere. where some_code_to_update_the_dialog are several functions which are executed one after one. Problem FOR NOW is that each function need ages to get the data and my GUI is dead while running the functions... If there is nothing to do, the CPU load is 100% while running the loop. Oops! Not realy what I want! So, can anyone provide me with a Tutorial HOW TO MULTITHREAD those functions some_code_to_update_the_dialog ? Ok, im still here...hope i understand...u get in to the update function...and update some widgets!?? - or what... ...or it just takes to much time to calculate the data, and for that time your (main-)thread cannot update the gui. Then it should work as i said... -signal to update data starts 2nd thread -main thread does normal gui response -2nd thread got data -2nd thread comes into situation to have to lock (thats that thing i have had a link for) some gui-data so main thread does not conflict with or -me is false and i am just to happy with talking to myself ;) And maybe some other tips... Thanks, Greetings and nice Day Michelle Konzack Systemadministrator Tamay Dogan Network Debian GNU/Linux Consultant -- Linux-User #280138 with the Linux Counter, http://counter.li.org/ # Debian GNU/Linux Consultant # Michelle Konzack Apt. 917 ICQ #328449886 50, rue de Soultz MSN LinuxMichi 0033/6/6192519367100 Strasbourg/France IRC #Debian (irc.icq.com) ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list ___ Der frühe Vogel fängt den Wurm. Hier gelangen Sie zum neuen Yahoo! Mail: http://mail.yahoo.de ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Gtk+ and multithreating
Hello *, I am new on this list, but have already codes some VERY simple admin GUI's... Now I have a bigger problem/Application which need definitiv multithreading since it must poll several servers and update several TreeView and ListView. I have no experience with it, since I am one of those console-singel-task-no-one-bother-me programmers (Debian GNU/Linux)... :-) OK, for now my GUI is running and then I use a loop like 8 window_main=create_window_main(); while ( TRUE ) { while ( gtk_events_pending() ) gtk_main_iteration(); some_code_to_update_the_dialog } 8 where some_code_to_update_the_dialog are several functions which are executed one after one. Problem FOR NOW is that each function need ages to get the data and my GUI is dead while running the functions... If there is nothing to do, the CPU load is 100% while running the loop. Oops! Not realy what I want! So, can anyone provide me with a Tutorial HOW TO MULTITHREAD those functions some_code_to_update_the_dialog ? And maybe some other tips... Thanks, Greetings and nice Day Michelle Konzack Systemadministrator Tamay Dogan Network Debian GNU/Linux Consultant -- Linux-User #280138 with the Linux Counter, http://counter.li.org/ # Debian GNU/Linux Consultant # Michelle Konzack Apt. 917 ICQ #328449886 50, rue de Soultz MSN LinuxMichi 0033/6/6192519367100 Strasbourg/France IRC #Debian (irc.icq.com) ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: Gtk+ and multithreating
On Mon, 2007-05-14 at 19:53 +0200, Michelle Konzack wrote: So, can anyone provide me with a Tutorial HOW TO MULTITHREAD those functions some_code_to_update_the_dialog ? And maybe some other tips... Can you do everything asynchronously? Initiate your connections, and set up some GIOChannels to watch for data availability? That will let you set up callbacks so the GLib main loop will notify your program as bits of data are available from each server. You don't block on communication, your GUI stays responsive, and you don't have to worry about the complexities of multithreading. - Michael -- Michael Ekstrand Research Assistant, Scalable Computing Laboratory Goanna, compute cluster and InfiniBand network monitor tool: http://www.scl.ameslab.gov/Projects/Monitor/ ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
Re: Gtk+ and multithreating
Le lundi 14 mai 2007 20:20, Michael Ekstrand a écrit : On Mon, 2007-05-14 at 19:53 +0200, Michelle Konzack wrote: So, can anyone provide me with a Tutorial HOW TO MULTITHREAD those functions some_code_to_update_the_dialog ? And maybe some other tips... Can you do everything asynchronously? Initiate your connections, and set up some GIOChannels to watch for data availability? That will let you set up callbacks so the GLib main loop will notify your program as bits of data are available from each server. You don't block on communication, your GUI stays responsive, and you don't have to worry about the complexities of multithreading. - Michael Michael is probably right, but to answer your question there's a good documentation about that in the GLib manual. See the sections 'Threads', 'Thread Pools' and 'Asynchronous Queues' under 'Glib Core Application Support'. If you want to know more, there's a tutorial here (about pthread but with a general introduction) : http://www.llnl.gov/computing/tutorials/pthreads -- Cédric Lucantis ___ gtk-app-devel-list mailing list gtk-app-devel-list@gnome.org http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list