blob: 2de7689176bce9c276aac1f82bafcf3a02d8c55e [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001// Create threads in such a way that there is a realistic chance that the
2// parent thread finishes before the created thread finishes.
3
sewardjc68cbe32007-11-27 01:59:38 +00004
5#include <assert.h>
6#include <stdlib.h>
7#include <stdio.h>
sewardjaf44c822007-11-25 14:01:38 +00008#include <pthread.h>
9
sewardjc68cbe32007-11-27 01:59:38 +000010
sewardjaf44c822007-11-25 14:01:38 +000011static pthread_t s_thread[1000];
12static int s_arg[1000];
13
14static void* thread_func(void* p)
15{
sewardjc68cbe32007-11-27 01:59:38 +000016 int thread_count = *(int*)(p);
sewardjaf44c822007-11-25 14:01:38 +000017 if (thread_count > 0)
18 {
19 thread_count--;
20 // std::cout << "create " << thread_count << std::endl;
21 s_arg[thread_count] = thread_count;
22 pthread_create(&s_thread[thread_count], 0, thread_func,
bart91740e22008-03-11 20:06:50 +000023 &s_arg[thread_count]);
sewardjaf44c822007-11-25 14:01:38 +000024#if 0
25 std::cout << "created " << thread_count << "(" << s_thread[thread_count]
bart91740e22008-03-11 20:06:50 +000026 << ")" << std::endl;
sewardjaf44c822007-11-25 14:01:38 +000027#endif
28 }
29 return 0;
30}
31
32int main(int argc, char** argv)
33{
bartc45fec32008-05-01 15:39:18 +000034 unsigned thread_count;
sewardjc68cbe32007-11-27 01:59:38 +000035 int i;
36
37 thread_count = argc > 1 ? atoi(argv[1]) : 50;
sewardjaf44c822007-11-25 14:01:38 +000038 assert(thread_count <= sizeof(s_thread) / sizeof(s_thread[0]));
39 assert(thread_count >= 1);
40 thread_count--;
41 // std::cout << "create " << thread_count << std::endl;
42 pthread_create(&s_thread[thread_count], 0, thread_func,
bart91740e22008-03-11 20:06:50 +000043 &thread_count);
sewardjaf44c822007-11-25 14:01:38 +000044#if 0
45 std::cout << "created " << thread_count << "(" << s_thread[thread_count]
bart91740e22008-03-11 20:06:50 +000046 << ")" << std::endl;
sewardjaf44c822007-11-25 14:01:38 +000047#endif
sewardjc68cbe32007-11-27 01:59:38 +000048 for (i = thread_count; i >= 0; i--)
sewardjaf44c822007-11-25 14:01:38 +000049 {
50 // std::cout << "join " << i << "(" << s_thread[i] << ")" << std::endl;
51 pthread_join(s_thread[i], 0);
52 }
53 return 0;
54}