Re: When to use gdk_threads_init?



The whole program is as follows:

#include <iostream>
#include <gtk/gtk.h>

static gboolean button_press_callback(GtkWidget *window, GdkEventButton
*event, gpointer data)
{
std::cout << "in press" << std::endl;
gdk_threads_enter();
std::cout << "threads enter" << std::endl;
gdk_threads_leave();
std::cout << "threads leave" << std::endl;
return TRUE;
}

int main(int argc, char *argv[])
{
if(!g_thread_supported()) {
g_thread_init(NULL);
}
gdk_threads_init();
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(G_OBJECT(window), "delete-event",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_add_events(window, GDK_BUTTON_PRESS_MASK);
g_signal_connect(G_OBJECT(window), "button-press-event",
G_CALLBACK(button_press_callback), NULL);
gtk_widget_show_all(window);
gdk_threads_enter();
gtk_main();
gdk_threads_leave();
return 0;
}


The source file is saved as tg.cpp.
Compile it with:
g++ -Wall tg.cpp -otg `pkg-config --cflags --libs gtk+-2.0 gthread-2.0`

No error or warning generated.
In ubuntu:
And when use gdk_threas_init() in main function, when I pressed on the
window, only "in press" displayed and the whole window froze after that.
After remove gdk_threads_init() in main function, all things goes well, "in
press", "threads enter" and "threads leave" display every time I click on
the window.

In windows (compiled with VS2008):
It works well with gdk_threads_init().

--------------------------------------------------

Hello, I'm new to GTK. I am using gthread with gtk both in Windows and Ubuntu.
When I using it in Windows, [...] it works well.

That sounds strange, as using GTK+ from multiple threads definitely
does not work in general on Windows. (Some unknown subset of GTK+
calls might work even from multiple threads) So are you just calling
GLib functions from multiple threads?

Please distill your problem down to a minimal but complete sample
program that people can look at.

--tml



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