1 /* Create threads from multiple threads in parallel.
2 Copyright 2007-2014 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 thread_function (void *arg)
29 int x = * (int *) arg;
31 printf ("Thread <%d> executing\n", x);
37 create_function (void *arg)
40 pthread_t threads[NUM_THREAD];
42 int i = * (int *) arg;
45 pthread_attr_init (&attr); /* set breakpoint 1 here. */
46 pthread_attr_setstacksize (&attr, 2*PTHREAD_STACK_MIN);
48 /* Create a ton of quick-executing threads, then wait for them to
50 for (j = 0; j < NUM_THREAD; ++j)
52 args[j] = i * 1000 + j;
53 pthread_create (&threads[j], &attr, thread_function, &args[j]);
56 for (j = 0; j < NUM_THREAD; ++j)
57 pthread_join (threads[j], NULL);
59 pthread_attr_destroy (&attr);
65 main (int argc, char **argv)
68 pthread_t threads[NUM_CREATE];
72 pthread_attr_init (&attr);
73 pthread_attr_setstacksize (&attr, 2*PTHREAD_STACK_MIN);
75 for (n = 0; n < 100; ++n)
77 for (i = 0; i < NUM_CREATE; i++)
80 pthread_create (&threads[i], &attr, create_function, &args[i]);
84 for (i = 0; i < NUM_CREATE; i++)
85 pthread_join (threads[i], NULL);
88 pthread_attr_destroy (&attr);