]>
Commit | Line | Data |
---|---|---|
62a24a2b | 1 | /* Create threads from multiple threads in parallel. |
1d506c26 | 2 | Copyright 2007-2024 Free Software Foundation, Inc. |
62a24a2b DJ |
3 | |
4 | This file is part of GDB. | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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/>. */ | |
18 | ||
19 | #include <pthread.h> | |
20 | #include <stdio.h> | |
21 | #include <limits.h> | |
22 | ||
23 | #define NUM_CREATE 1 | |
24 | #define NUM_THREAD 8 | |
25 | ||
26 | void * | |
27 | thread_function (void *arg) | |
28 | { | |
29 | int x = * (int *) arg; | |
30 | ||
31 | printf ("Thread <%d> executing\n", x); | |
32 | ||
33 | return NULL; | |
34 | } | |
35 | ||
36 | void * | |
37 | create_function (void *arg) | |
38 | { | |
39 | pthread_attr_t attr; | |
40 | pthread_t threads[NUM_THREAD]; | |
41 | int args[NUM_THREAD]; | |
3c75f00a | 42 | size_t stacksize; |
62a24a2b DJ |
43 | int i = * (int *) arg; |
44 | int j; | |
45 | ||
46 | pthread_attr_init (&attr); /* set breakpoint 1 here. */ | |
3c75f00a JB |
47 | pthread_attr_getstacksize (&attr, &stacksize); |
48 | pthread_attr_setstacksize (&attr, 2 * stacksize); | |
62a24a2b DJ |
49 | |
50 | /* Create a ton of quick-executing threads, then wait for them to | |
51 | complete. */ | |
52 | for (j = 0; j < NUM_THREAD; ++j) | |
53 | { | |
54 | args[j] = i * 1000 + j; | |
55 | pthread_create (&threads[j], &attr, thread_function, &args[j]); | |
56 | } | |
57 | ||
58 | for (j = 0; j < NUM_THREAD; ++j) | |
59 | pthread_join (threads[j], NULL); | |
60 | ||
61 | pthread_attr_destroy (&attr); | |
62 | ||
63 | return NULL; | |
64 | } | |
65 | ||
66 | int | |
67 | main (int argc, char **argv) | |
68 | { | |
69 | pthread_attr_t attr; | |
70 | pthread_t threads[NUM_CREATE]; | |
71 | int args[NUM_CREATE]; | |
3c75f00a | 72 | size_t stacksize; |
62a24a2b DJ |
73 | int n, i; |
74 | ||
75 | pthread_attr_init (&attr); | |
3c75f00a JB |
76 | pthread_attr_getstacksize (&attr, &stacksize); |
77 | pthread_attr_setstacksize (&attr, 2 * stacksize); | |
62a24a2b DJ |
78 | |
79 | for (n = 0; n < 100; ++n) | |
80 | { | |
81 | for (i = 0; i < NUM_CREATE; i++) | |
82 | { | |
83 | args[i] = i; | |
84 | pthread_create (&threads[i], &attr, create_function, &args[i]); | |
85 | } | |
86 | ||
87 | create_function (&i); | |
88 | for (i = 0; i < NUM_CREATE; i++) | |
89 | pthread_join (threads[i], NULL); | |
90 | } | |
91 | ||
92 | pthread_attr_destroy (&attr); | |
93 | ||
94 | return 0; | |
95 | } |