]> Pileus Git - ~andy/gtk/blob - tests/testthreads.c
Use gtk_box_new() instead gtk_[v|h]box_new()
[~andy/gtk] / tests / testthreads.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <gtk/gtk.h>
30 #include "config.h"
31
32 #ifdef USE_PTHREADS
33 #include <pthread.h>
34
35 static int nthreads = 0;
36 static pthread_mutex_t nthreads_mutex = PTHREAD_MUTEX_INITIALIZER;
37
38 void
39 close_cb (GtkWidget *w, gint *flag)
40 {
41   *flag = 1;
42 }
43
44 gint
45 delete_cb (GtkWidget *w, GdkEvent *event, gint *flag)
46 {
47   *flag = 1;
48   return TRUE;
49 }
50
51 void *
52 counter (void *data)
53 {
54   gchar *name = data;
55   gint flag = 0;
56   gint counter = 0;
57   gchar buffer[32];
58   
59   GtkWidget *window;
60   GtkWidget *vbox;
61   GtkWidget *label;
62   GtkWidget *button;
63
64   gdk_threads_enter();
65
66   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
67   gtk_window_set_title (GTK_WINDOW (window), name);
68   gtk_widget_set_size_request (window, 100, 50);
69
70   vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 0);
71
72   g_signal_connect (window, "delete-event",
73                     G_CALLBACK (delete_cb), &flag);
74
75   gtk_container_add (GTK_CONTAINER (window), vbox);
76
77   label = gtk_label_new ("0");
78   gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, FALSE, 0);
79
80   button = gtk_button_new_with_label ("Close");
81   g_signal_connect (button, "clicked",
82                     G_CALLBACK (close_cb), &flag);
83   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
84
85   gtk_widget_show_all (window);
86
87   /* Since flag is only checked or set inside the GTK lock,
88    * we don't have to worry about locking it explicitly
89    */
90   while (!flag)
91     {
92       sprintf(buffer, "%d", counter);
93       gtk_label_set_text (GTK_LABEL (label), buffer);
94       gdk_threads_leave();
95       counter++;
96       /* Give someone else a chance to get the lock next time.
97        * Only necessary because we don't do anything else while
98        * releasing the lock.
99        */
100       sleep(0);
101       
102       gdk_threads_enter();
103     }
104
105   gtk_widget_destroy (window);
106
107   pthread_mutex_lock (&nthreads_mutex);
108   nthreads--;
109   if (nthreads == 0)
110     gtk_main_quit();
111   pthread_mutex_unlock (&nthreads_mutex);
112
113   gdk_threads_leave();
114
115   return NULL;
116 }
117
118 #endif
119
120 int 
121 main (int argc, char **argv)
122 {
123 #ifdef USE_PTHREADS
124   int i;
125
126   if (!gdk_threads_init())
127     {
128       fprintf(stderr, "Could not initialize threads\n");
129       exit(1);
130     }
131
132   gtk_init (&argc, &argv);
133
134   pthread_mutex_lock (&nthreads_mutex);
135
136   for (i=0; i<5; i++)
137     {
138       char buffer[5][10];
139       pthread_t thread;
140       
141       sprintf(buffer[i], "Thread %i", i);
142       if (pthread_create (&thread, NULL, counter, buffer[i]))
143         {
144           fprintf(stderr, "Couldn't create thread\n");
145           exit(1);
146         }
147       nthreads++;
148     }
149
150   pthread_mutex_unlock (&nthreads_mutex);
151
152   gdk_threads_enter();
153   gtk_main();
154   gdk_threads_leave();
155   fprintf(stderr, "Done\n");
156 #else /* !USE_PTHREADS */
157   fprintf (stderr, "GTK+ not compiled with threads support\n");
158   exit (1);
159 #endif /* USE_PTHREADS */  
160
161   return 0;
162 }