Created
October 4, 2011 05:32
-
-
Save jiangmiao/1260967 to your computer and use it in GitHub Desktop.
run gtk main loop in sub thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// hello.cc | |
// g++ -std=c++0x hello.cc `pkg-config --libs --cflags gtk+-2.0` && ./a.out | |
#include <thread> | |
#include <functional> | |
#include <gtk/gtk.h> | |
bool done = false; | |
void quit(GtkWidget *, gpointer) | |
{ | |
done = true; | |
gtk_main_quit(); | |
} | |
void destroy(GtkWidget *, gpointer window) | |
{ | |
gtk_widget_destroy(GTK_WIDGET(window)); | |
} | |
void run(int argc, char *argv[]) | |
{ | |
GtkWidget *window, *button; | |
gtk_init(&argc, &argv); | |
window = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
button = gtk_button_new_with_label("exit"); | |
gtk_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(quit), NULL); | |
gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(destroy), window); | |
gtk_container_add(GTK_CONTAINER(window), button); | |
gtk_widget_show_all(window); | |
gtk_main(); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
std::thread thrd(std::bind(run, argc, argv)); | |
int i=0; | |
while (!done) | |
{ | |
printf("%d\n", ++i); | |
sleep(1); | |
} | |
thrd.join(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment