1 // Create threads in such a way that there is a realistic chance that the
2 // parent thread finishes before the created thread finishes.
6 #include <limits.h> /* PTHREAD_STACK_MIN */
13 static pthread_t s_thread
[1000];
14 static int s_arg
[1000];
16 static void* thread_func(void* p
)
18 int thread_count
= *(int*)(p
);
24 // std::cout << "create " << thread_count << std::endl;
25 s_arg
[thread_count
] = thread_count
;
26 pthread_attr_init(&attr
);
27 #if !defined(VGO_freebsd)
28 pthread_attr_setstacksize(&attr
, PTHREAD_STACK_MIN
);
30 pthread_create(&s_thread
[thread_count
], &attr
, thread_func
,
31 &s_arg
[thread_count
]);
32 pthread_attr_destroy(&attr
);
34 std::cout
<< "created " << thread_count
<< "(" << s_thread
[thread_count
]
41 int main(int argc
, char** argv
)
44 unsigned thread_count
;
47 thread_count
= argc
> 1 ? atoi(argv
[1]) : 50;
48 assert(thread_count
<= sizeof(s_thread
) / sizeof(s_thread
[0]));
49 assert(thread_count
>= 1);
51 // std::cout << "create " << thread_count << std::endl;
52 pthread_attr_init(&attr
);
53 #if !defined(VGO_freebsd)
54 pthread_attr_setstacksize(&attr
, PTHREAD_STACK_MIN
);
56 pthread_create(&s_thread
[thread_count
], &attr
, thread_func
,
58 pthread_attr_destroy(&attr
);
60 std::cout
<< "created " << thread_count
<< "(" << s_thread
[thread_count
]
63 for (i
= thread_count
; i
>= 0; i
--)
65 // std::cout << "join " << i << "(" << s_thread[i] << ")" << std::endl;
66 pthread_join(s_thread
[i
], 0);