Patch to threads example



Hi,

The attached patch updates the example for using threads in 
docs/faq/gtk-faq.sgml and docs/reference/gdk/tmpl/threads.sgml.

I'm not able to build docs from this machine so you probably should look
at it.

Benjamin



Index: docs/faq/gtk-faq.sgml
===================================================================
RCS file: /cvs/gnome/gtk+/docs/faq/gtk-faq.sgml,v
retrieving revision 1.13
diff -u -r1.13 gtk-faq.sgml
--- docs/faq/gtk-faq.sgml	2001/02/27 13:48:36	1.13
+++ docs/faq/gtk-faq.sgml	2002/03/17 21:20:29
@@ -2,7 +2,7 @@
 <book>
 
 <bookinfo>
-  <date>February 27th 2001</date>
+  <date>March 17th 2002</date>
   <title>GTK+ FAQ</title>
   <authorgroup>
     <author>
@@ -1246,91 +1246,61 @@
 <programlisting role="C">
 /*-------------------------------------------------------------------------
  * Filename:      gtk-thread.c
- * Version:       0.99.1
+ * Version:       0.99.2
  * Copyright:     Copyright (C) 1999, Erik Mouw
- * Author:        Erik Mouw &lt;J A K Mouw its tudelft nl&gt;
+ * Author:        Erik Mouw &lt;J.A.K.Mouwits.tudelft.nl&gt;
  * Description:   GTK threads example. 
  * Created at:    Sun Oct 17 21:27:09 1999
- * Modified by:   Erik Mouw &lt;J A K Mouw its tudelft nl&gt;
+ * Modified by:   Erik Mouw &lt;J.A.K.Mouwits.tudelft.nl&gt;
  * Modified at:   Sun Oct 24 17:21:41 1999
+ * Modified by:   Benjamin Otte &lt;in7y118 public uni-hamburg de&gt;
+ * Modified at:   Sun Mar 17 21:26:35 2002
  *-----------------------------------------------------------------------*/
 /*
  * Compile with:
  *
- * cc -o gtk-thread gtk-thread.c `gtk-config --cflags --libs gthread`
+ * cc -o gtk-thread gtk-thread.c `pkg-config --libs --cflags gtk+-2.0 gthread-2.0`
  *
  * Thanks to Sebastian Wilhelmi and Owen Taylor for pointing out some
  * bugs.
  *
  */
 
-#include &lt;stdio.h&gt;
-#include &lt;stdlib.h&gt;
-#include &lt;unistd.h&gt;
-#include &lt;time.h&gt;
 #include &lt;gtk/gtk.h&gt;
-#include &lt;glib.h&gt;
-#include &lt;pthread.h&gt;
 
-#define YES_IT_IS    (1)
-#define NO_IT_IS_NOT (0)
-
 typedef struct 
 {
   GtkWidget *label;
-  int what;
+  gchar *what;
 } yes_or_no_args;
-
-G_LOCK_DEFINE_STATIC (yes_or_no);
-static volatile int yes_or_no = YES_IT_IS;
 
-void destroy(GtkWidget *widget, gpointer data)
+void 
+destroy(GtkWidget *widget, gpointer data)
 {
   gtk_main_quit();
 }
 
-void *argument_thread(void *args)
+gpointer 
+argument_thread(gpointer args)
 {
-  yes_or_no_args *data = (yes_or_no_args *)args;
-  gboolean say_something;
+  yes_or_no_args *data = (yes_or_no_args *) args;
 
   for(;;)
-    {
-      /* sleep a while */
-      sleep(rand() / (RAND_MAX / 3) + 1);
-
-      /* lock the yes_or_no_variable */
-      G_LOCK(yes_or_no);
-
-      /* do we have to say something? */
-      say_something = (yes_or_no != data->what);
-
-      if(say_something)
-	{
-	  /* set the variable */
-	  yes_or_no = data->what;
-	}
-
-      /* Unlock the yes_or_no variable */
-      G_UNLOCK(yes_or_no);
-
-      if(say_something)
-	{
-	  /* get GTK thread lock */
-	  gdk_threads_enter();
-
-	  /* set label text */
-	  if(data->what == YES_IT_IS)
-	    gtk_label_set_text(GTK_LABEL(data->label), "O yes, it is!");
-	  else
-	    gtk_label_set_text(GTK_LABEL(data->label), "O no, it isn't!");
-
-	  /* release GTK thread lock */
-	  gdk_threads_leave();
-	}
-    }
+  {
+    /* sleep a while (maximum 1 second) */
+    g_usleep(g_random_int_range (0, 1000 * 1000) + 1);
+
+    /* get GTK thread lock */
+    gdk_threads_enter();
+
+    /* set label text */
+    gtk_label_set_text(GTK_LABEL(data-&gt;label), data-&gt;what);
+
+    /* release GTK thread lock */
+    gdk_threads_leave();
+  }
 
-  return(NULL);
+  return NULL;
 }
 
 int main(int argc, char *argv[])
@@ -1338,10 +1308,10 @@
   GtkWidget *window;
   GtkWidget *label;
   yes_or_no_args yes_args, no_args;
-  pthread_t no_tid, yes_tid;
 
   /* init threads */
   g_thread_init(NULL);
+  gdk_threads_init();
 
   /* init gtk */
   gtk_init(&amp;argc, &amp;argv);
@@ -1367,12 +1337,12 @@
 
   /* create the threads */
   yes_args.label = label;
-  yes_args.what = YES_IT_IS;
-  pthread_create(&amp;yes_tid, NULL, argument_thread, &amp;yes_args);
+  yes_args.what = "Yes it is";
+  g_thread_create(argument_thread, &amp;yes_args, FALSE, NULL);
 
   no_args.label = label;
-  no_args.what = NO_IT_IS_NOT;
-  pthread_create(&amp;no_tid, NULL, argument_thread, &amp;no_args);
+  no_args.what = "No it is not";
+  g_thread_create(argument_thread, &amp;no_args, FALSE, NULL);
 
   /* enter the GTK main loop */
   gdk_threads_enter();
Index: docs/reference/gdk/tmpl/threads.sgml
===================================================================
RCS file: /cvs/gnome/gtk+/docs/reference/gdk/tmpl/threads.sgml,v
retrieving revision 1.8
diff -u -r1.8 threads.sgml
--- docs/reference/gdk/tmpl/threads.sgml	2002/01/10 00:11:31	1.8
+++ docs/reference/gdk/tmpl/threads.sgml	2002/03/17 21:20:30
@@ -86,91 +86,61 @@
 <programlisting role="C">
 /*-------------------------------------------------------------------------
  * Filename:      gtk-thread.c
- * Version:       0.99.1
+ * Version:       0.99.2
  * Copyright:     Copyright (C) 1999, Erik Mouw
- * Author:        Erik Mouw &lt;J A K Mouw its tudelft nl&gt;
+ * Author:        Erik Mouw &lt;J.A.K.Mouwits.tudelft.nl&gt;
  * Description:   GTK threads example. 
  * Created at:    Sun Oct 17 21:27:09 1999
- * Modified by:   Erik Mouw &lt;J A K Mouw its tudelft nl&gt;
+ * Modified by:   Erik Mouw &lt;J.A.K.Mouwits.tudelft.nl&gt;
  * Modified at:   Sun Oct 24 17:21:41 1999
+ * Modified by:   Benjamin Otte &lt;in7y118 public uni-hamburg de&gt;
+ * Modified at:   Sun Mar 17 21:26:35 2002
  *-----------------------------------------------------------------------*/
 /*
  * Compile with:
  *
- * cc -o gtk-thread gtk-thread.c `gtk-config --cflags --libs gthread`
+ * cc -o gtk-thread gtk-thread.c `pkg-config --libs --cflags gtk+-2.0 gthread-2.0`
  *
  * Thanks to Sebastian Wilhelmi and Owen Taylor for pointing out some
  * bugs.
  *
  */
 
-#include &lt;stdio.h&gt;
-#include &lt;stdlib.h&gt;
-#include &lt;unistd.h&gt;
-#include &lt;time.h&gt;
 #include &lt;gtk/gtk.h&gt;
-#include &lt;glib.h&gt;
-#include &lt;pthread.h&gt;
 
-#define YES_IT_IS    (1)
-#define NO_IT_IS_NOT (0)
-
 typedef struct 
 {
   GtkWidget *label;
-  int what;
+  gchar *what;
 } yes_or_no_args;
-
-G_LOCK_DEFINE_STATIC (yes_or_no);
-static volatile int yes_or_no = YES_IT_IS;
 
-void destroy(GtkWidget *widget, gpointer data)
+void 
+destroy(GtkWidget *widget, gpointer data)
 {
   gtk_main_quit();
 }
 
-void *argument_thread(void *args)
+gpointer 
+argument_thread(gpointer args)
 {
-  yes_or_no_args *data = (yes_or_no_args *)args;
-  gboolean say_something;
+  yes_or_no_args *data = (yes_or_no_args *) args;
 
   for(;;)
-    {
-      /* sleep a while */
-      sleep(rand() / (RAND_MAX / 3) + 1);
-
-      /* lock the yes_or_no_variable */
-      G_LOCK(yes_or_no);
-
-      /* do we have to say something? */
-      say_something = (yes_or_no != data->what);
-
-      if(say_something)
-	{
-	  /* set the variable */
-	  yes_or_no = data->what;
-	}
-
-      /* Unlock the yes_or_no variable */
-      G_UNLOCK(yes_or_no);
-
-      if(say_something)
-	{
-	  /* get GTK thread lock */
-	  gdk_threads_enter();
-
-	  /* set label text */
-	  if(data->what == YES_IT_IS)
-	    gtk_label_set_text(GTK_LABEL(data->label), "O yes, it is!");
-	  else
-	    gtk_label_set_text(GTK_LABEL(data->label), "O no, it isn't!");
-
-	  /* release GTK thread lock */
-	  gdk_threads_leave();
-	}
-    }
+  {
+    /* sleep a while (maximum 1 second) */
+    g_usleep(g_random_int_range (0, 1000 * 1000) + 1);
+
+    /* get GTK thread lock */
+    gdk_threads_enter();
+
+    /* set label text */
+    gtk_label_set_text(GTK_LABEL(data-&gt;label), data-&gt;what);
+
+    /* release GTK thread lock */
+    gdk_threads_leave();
+  }
 
-  return(NULL);
+  return NULL;
 }
 
 int main(int argc, char *argv[])
@@ -178,11 +148,10 @@
   GtkWidget *window;
   GtkWidget *label;
   yes_or_no_args yes_args, no_args;
-  pthread_t no_tid, yes_tid;
 
   /* init threads */
   g_thread_init(NULL);
-  gdk_threads_init ();
+  gdk_threads_init();
 
   /* init gtk */
   gtk_init(&amp;argc, &amp;argv);
@@ -208,12 +177,12 @@
 
   /* create the threads */
   yes_args.label = label;
-  yes_args.what = YES_IT_IS;
-  pthread_create(&amp;yes_tid, NULL, argument_thread, &amp;yes_args);
+  yes_args.what = "Yes it is";
+  g_thread_create(argument_thread, &amp;yes_args, FALSE, NULL);
 
   no_args.label = label;
-  no_args.what = NO_IT_IS_NOT;
-  pthread_create(&amp;no_tid, NULL, argument_thread, &amp;no_args);
+  no_args.what = "No it is not";
+  g_thread_create(argument_thread, &amp;no_args, FALSE, NULL);
 
   /* enter the GTK main loop */
   gdk_threads_enter();


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