Re: Threads



Olexiy Avramchenko wrote:
Russell Shaw wrote:

Hi,
In the faq on threads (http://www.gtk.org/faq/#AEN482), there is:

int
main (int argc, char *argv[])
{
  GtkWidget *window;

  /* init threads */     g_thread_init(NULL);
  gdk_threads_init();

  /* init gtk */
  gtk_init(&argc, &argv);

  window = create_window();
  gtk_widget_show(window);

  gdk_threads_enter();
  gtk_main();
  gdk_threads_leave();

  return 0;
}

Because gdk_threads_enter()/leave() surround gtk_main(), then
wouldn't that mean other threads that use gdk_threads_enter()/leave()
are always blocked until gtk_main() exits?


Hello,
No, gtk_main unlocks mutex before g_main_loop_run() call and then GDK code locks it just before gtk_main_do_event() call. So, mutex is locked only when event is processing.

silly me, i did a case-sensitive grep for threads.

Ok, in gtkmain.c:

void
gtk_main (void)
{
  ...
  if (g_main_loop_is_running (main_loops->data))
    {
      GDK_THREADS_LEAVE ();
      g_main_loop_run (loop);
      GDK_THREADS_ENTER ();
      gdk_flush ();
    }
  ...
}

So if g_main_loop_run() runs for the duration of the app, then there is nothing
guarding gdk calls made by gtk callbacks. So, doesn't this mean that every gtk
operation in a gtk callback that eventually calls a gdk function, should be
surrounded by gdk_threads_enter()/leave() ?



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