Re: Threads



Tristan Van Berkom wrote:
On Thu, 16 Dec 2004 02:47:50 +1100, Russell Shaw <rjshaw netspace net au> wrote:

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() ?


Callbacks that you register with widgets run outside the threads mutex,
so yes; you should protect gtk/gdk function calls with
gdk_threads_enter()/leave().

This is what is confusing:

http://www.gtk.org/faq/#AEN482

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;
}

Quote:

  Callbacks require a bit of attention. Callbacks from GTK+ (signals) are made
  within the GTK+ lock.   However callbacks from GLib (timeouts, IO callbacks,
  and idle functions) are made outside of the GTK+ lock. So, within a signal
  handler you do not need to call gdk_threads_enter(), but within the other types
  of callbacks, you do.

From what i see, GTK has *no* locks on gdk when a gtk callback happens,
because of this in gtk_main():

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

GDK_THREADS_LEAVE() releases any gtk exclusivity on gdk then the
app sits in g_main_loop_run().

GDK_THREADS_ENTER() is just a global g_mutex_lock(gdk_threads_mutex).


The FAQ is wrong isn't it?



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