1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GTK+ Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
33 static int nthreads = 0;
34 static pthread_mutex_t nthreads_mutex = PTHREAD_MUTEX_INITIALIZER;
37 close_cb (GtkWidget *w, gint *flag)
43 delete_cb (GtkWidget *w, GdkEvent *event, gint *flag)
64 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
65 gtk_window_set_title (GTK_WINDOW (window), name);
66 gtk_widget_set_size_request (window, 100, 50);
68 vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 0);
70 g_signal_connect (window, "delete-event",
71 G_CALLBACK (delete_cb), &flag);
73 gtk_container_add (GTK_CONTAINER (window), vbox);
75 label = gtk_label_new ("0");
76 gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, FALSE, 0);
78 button = gtk_button_new_with_label ("Close");
79 g_signal_connect (button, "clicked",
80 G_CALLBACK (close_cb), &flag);
81 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
83 gtk_widget_show_all (window);
85 /* Since flag is only checked or set inside the GTK lock,
86 * we don't have to worry about locking it explicitly
90 sprintf(buffer, "%d", counter);
91 gtk_label_set_text (GTK_LABEL (label), buffer);
94 /* Give someone else a chance to get the lock next time.
95 * Only necessary because we don't do anything else while
103 gtk_widget_destroy (window);
105 pthread_mutex_lock (&nthreads_mutex);
109 pthread_mutex_unlock (&nthreads_mutex);
119 main (int argc, char **argv)
124 if (!gdk_threads_init())
126 fprintf(stderr, "Could not initialize threads\n");
130 gtk_init (&argc, &argv);
132 pthread_mutex_lock (&nthreads_mutex);
139 sprintf(buffer[i], "Thread %i", i);
140 if (pthread_create (&thread, NULL, counter, buffer[i]))
142 fprintf(stderr, "Couldn't create thread\n");
148 pthread_mutex_unlock (&nthreads_mutex);
153 fprintf(stderr, "Done\n");
154 #else /* !USE_PTHREADS */
155 fprintf (stderr, "GTK+ not compiled with threads support\n");
157 #endif /* USE_PTHREADS */