1
void forloop2()
{
    int i = 0;
    while(TRUE)
    {
        printf("forloop2\n");
    }
}

int main() {
    GtkWidget *window;
    g_thread_init(NULL);
    gdk_threads_init();
    g_thread_create((GThreadFunc)forloop2, NULL, FALSE, NULL);
    gtk_init(NULL, NULL);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_widget_show_all (window);
    gtk_main();
}

It seems the created thread affects gtk_window_new(my programe hangs here),

how do I do it correctly?

UPDATE

fixed by gdk_threads_enter/leave

httpinterpret
  • 6,409
  • 9
  • 33
  • 37
  • It works fine for me. I added `gtk_container_add (GTK_CONTAINER (window), gtk_entry_new ());` there to test that window works, and it does. –  May 09 '10 at 12:57
  • Do you mean it works without `gdk_threads_enter/leave`, really? – httpinterpret May 09 '10 at 14:45
  • That's the point with multithreading bugs; they can work perfectly fine on one system and crash horribly on the next. – ptomato May 09 '10 at 21:28
  • 2
    You should add an aswer with your solution and then accept it yourself. – kazanaki Aug 20 '10 at 13:05

1 Answers1

1

From the GDK docs:

You must also surround any calls to GTK+ not made within a signal handler with a gdk_threads_enter()/gdk_threads_leave() pair.

See http://developer.gnome.org/gdk/stable/gdk-Threads.html#gdk-Threads.description

cmende
  • 397
  • 2
  • 11
  • See also this answer, http://stackoverflow.com/questions/2793293/how-do-i-create-a-new-thread-to-make-pcap-loop-and-gtk-main-compatible/2793644#2793644 – hlovdal Sep 12 '11 at 18:28