I'm doing some stuff with GTK+ in a separate thread, and properly locking with gdk_threads_enter/gdk_threads_leave() but the display doesn't update properly. [GTK 2.x]

GTK+ FAQ
<<< Previous | Development with GTK+: general questions | Next >>>

# I'm doing some stuff with GTK+ in a separate thread, and properly locking with gdk_threads_enter/gdk_threads_leave() but the display doesn't update properly. [GTK 2.x]

For efficiency, the X window system batches up commands and sends them to the X server in batches instead of sending out immediately.

In a non-multithreaded program, you don't have to worry about this, since the first thing that happens when control returns to the main loop is that any outstanding X requests are sent to the X server.

However, if you are making GTK+ calls from a thread other than the main loop, then GTK+ doesn't know when to send batched commands out. For that reason, after making GTK+ calls in a separate thread, it is usually a good idea to call gdk_flush() before gdk_thread_leave().

Actually, gdk_flush() is more expensive than is necessary here, since it waits for the X server to finish outstanding commands as well; if performance is an issue, you may want to call XFlush() directly:

    #include <gdk/gdkx.h>

    void my_flush_commands (void)
    {
      GdkDisplay *display = gdk_display_get_default ();
      XFlush (GDK_DISPLAY_XDISPLAY (display);
    }

<<< Previous | Home | Next >>>
Is GTK+ thread safe? How do I write multi-threaded GTK+ applications? [GTK 2.x] | Up | What's an easy way to run a function in the thread with the main loop? [GTK 2.x]