Commits

wodny  committed 5e60893

C++ examples.

  • Participants
  • Parent commits f3af2ab

Comments (0)

Files changed (4)

 syntax: glob
 
 *.pyc
+cpp/primarythread
+cpp/secondarythread

File cpp/Makefile

+all: primarythread secondarythread
+
+primarythread: primarythread.cpp
+	gcc primarythread.cpp -o primarythread `pkg-config --cflags --libs gtk+-2.0` -g
+
+secondarythread: secondarythread.cpp
+	gcc secondarythread.cpp -o secondarythread `pkg-config --cflags --libs gtk+-2.0` -g

File cpp/primarythread.cpp

+#include <gtk/gtk.h>
+#include <pthread.h>
+
+static void destroy( GtkWidget *widget,
+                     gpointer   data )
+{
+    gtk_main_quit ();
+}
+
+int main( int   argc,
+          char *argv[] )
+{
+    GtkWidget *window;
+    GtkWidget *button;
+
+    g_thread_init (NULL);
+    gdk_threads_init ();
+    gdk_threads_enter ();
+
+    gtk_init (&argc, &argv);
+    
+    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+    
+    g_signal_connect (window, "destroy",
+		      G_CALLBACK (destroy), NULL);
+    
+    button = gtk_button_new_with_label ("Hello World");
+    
+    gtk_container_add (GTK_CONTAINER (window), button);
+    gtk_widget_show (button);
+    gtk_widget_show (window);
+
+    gdk_threads_leave ();
+
+    gdk_threads_enter ();
+    gtk_main ();
+    gdk_threads_leave ();
+    
+    return 0;
+}
+
+/* vim: set sw=4: */

File cpp/secondarythread.cpp

+#include <gtk/gtk.h>
+#include <pthread.h>
+
+static void destroy( GtkWidget *widget,
+                     gpointer   data )
+{
+    gtk_main_quit ();
+}
+
+void *second_thread (void *args)
+{
+    gdk_threads_enter ();
+    gtk_main ();
+    gdk_threads_leave ();
+}
+
+int main( int   argc,
+          char *argv[] )
+{
+    GtkWidget *window;
+    GtkWidget *button;
+
+    g_thread_init (NULL);
+    gdk_threads_init ();
+    gdk_threads_enter ();
+
+    gtk_init (&argc, &argv);
+    
+    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+    
+    g_signal_connect (window, "destroy",
+		      G_CALLBACK (destroy), NULL);
+    
+    button = gtk_button_new_with_label ("Hello World");
+    
+    gtk_container_add (GTK_CONTAINER (window), button);
+    gtk_widget_show (button);
+    gtk_widget_show (window);
+
+    gdk_threads_leave ();
+
+    pthread_t thread;
+    
+    pthread_create (&thread, NULL, second_thread, NULL);
+    pthread_join(thread, NULL);
+    
+    return 0;
+}
+
+/* vim: set sw=4: */