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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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.
27 static int nthreads = 0;
28 static pthread_mutex_t nthreads_mutex = PTHREAD_MUTEX_INITIALIZER;
31 close_cb (GtkWidget *w, gint *flag)
37 delete_cb (GtkWidget *w, GdkEvent *event, gint *flag)
58 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
59 gtk_window_set_title (GTK_WINDOW (window), name);
60 gtk_widget_set_usize (window, 100, 50);
62 vbox = gtk_vbox_new (FALSE, 0);
64 gtk_signal_connect (GTK_OBJECT (window), "delete_event",
65 GTK_SIGNAL_FUNC (delete_cb), &flag);
67 gtk_container_add (GTK_CONTAINER (window), vbox);
69 label = gtk_label_new ("0");
70 gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, FALSE, 0);
72 button = gtk_button_new_with_label ("Close");
73 gtk_signal_connect (GTK_OBJECT (button), "clicked",
74 GTK_SIGNAL_FUNC (close_cb), &flag);
75 gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
77 gtk_widget_show_all (window);
79 /* Since flag is only checked or set inside the GTK lock,
80 * we don't have to worry about locking it explicitly
84 sprintf(buffer, "%d", counter);
85 gtk_label_set (GTK_LABEL (label), buffer);
88 /* Give someone else a chance to get the lock next time.
89 * Only necessary because we don't do anything else while
97 gtk_widget_destroy (window);
99 pthread_mutex_lock (&nthreads_mutex);
103 pthread_mutex_unlock (&nthreads_mutex);
113 main (int argc, char **argv)
118 if (!gtk_threads_init())
120 fprintf(stderr, "Could not initialize threads\n");
124 gtk_init (&argc, &argv);
126 pthread_mutex_lock (&nthreads_mutex);
133 sprintf(buffer[i], "Thread %i", i);
134 if (pthread_create (&thread, NULL, counter, buffer[i]))
136 fprintf(stderr, "Couldn't create thread\n");
142 pthread_mutex_unlock (&nthreads_mutex);
147 fprintf(stderr, "Done\n");
148 #else /* !USE_PTHREADS */
149 fprintf (stderr, "GTK+ not compiled with threads support\n");
151 #endif /* USE_PTHREADS */