GTK usage within threads



I have spent lots of time looking at various informal and unofficial
documents and messages pertaining to using GDK and GTK in threads. I have
tried to follow the advise received, but I am not sure I have it right.

My threaded applications work OK and are reliable, but I sometimes
experience a performance problem: slow window updates: things hang dead for
a few seconds and then explode with activity. Sometimes I can trigger this
by generating unrelated mouse events or clicking scroll bars, etc. The
symptoms seem to be saying that the main loop is not running as frequently
as it needs and window updates are pending in buffers and not getting
written.

Here is what I am doing, in summary. I hope someone with more expertise can
take a few minutes to tell me if I am doing someting wrong.


main() program

   g_thread_init(0);
   gdk_threads_init();
   gtk_init(&argc, &argv);
   ...
   build main window, menus, toolbar, text_view window, etc.
   ...
   gtk_widget_show_all(mainWin);
   g_signal_connect(...)
   gtk_init_add(...);
   ...
   gdk_threads_enter();
   gtk_main();
   gdk_threads_leave();
   return 0;


main-functions() using GTK: 

   nothing special is done, with the following exception:
   if a main function must block itself due to a conflict 
   with a thread, the following is done to keep the main loop 
   running (and avoid locking-up menus and window updates):

   while (block-condition)
      while (gtk_events_pending()) gtk_main_iteration();
      usleep(1000)         // sleep 1 millisec.
      
   (I think the problem is here: if I eliminate the usleep()
    and make a hard loop instead, window updates progress 
    at high speed) (not a good solution for obvious reasons)


thread-functions() using GTK:

   gdk_threads_enter();
   ...
   gtk calls for dialogs or to update main window
   ...
   gdk_flush();
   gdk_threads_leave();
   return 0;


-- 
View this message in context: http://www.nabble.com/GTK-usage-within-threads-tf1944290.html#a5330269
Sent from the Gtk+ - Dev - General forum at Nabble.com.




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]