How do I make the child thread exit when the main thread exits?
void forloop2()
{
int i = 0;
while(TRUE)
{
printf("forloop2\n");
}
}
int main() {
GThread *Thread1;
GtkWidget *window;
g_thread_init(NULL);
gdk_threads_init();
gdk_threads_enter ();
Thread1 = g_thread_create((GThreadFunc)forloop2, NULL, TRUE, NULL);
gtk_init(NULL, NULL);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_show_all (window);
gtk_main();
g_thread_join(Thread1);
gdk_threads_leave ();
}
When I close the window, how do I Thread1
exit as well?
+2
a source to share
1 answer
Clean up basically the condition that the forloop2 () loop checks every iteration. When you want to exit main, set this condition and then call g_thread_join () on Thread1. Since forloop2 () checks when it sees this condition, it will exit, forcing it to join, and main will continue.
+1
a source to share