sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | // Create threads in such a way that there is a realistic chance that the |
| 2 | // parent thread finishes before the created thread finishes. |
| 3 | |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 4 | |
| 5 | #include <assert.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <stdio.h> |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 8 | #include <pthread.h> |
| 9 | |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 10 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 11 | static pthread_t s_thread[1000]; |
| 12 | static int s_arg[1000]; |
| 13 | |
| 14 | static void* thread_func(void* p) |
| 15 | { |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 16 | int thread_count = *(int*)(p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 17 | 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, |
bart | 91740e2 | 2008-03-11 20:06:50 +0000 | [diff] [blame] | 23 | &s_arg[thread_count]); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 24 | #if 0 |
| 25 | std::cout << "created " << thread_count << "(" << s_thread[thread_count] |
bart | 91740e2 | 2008-03-11 20:06:50 +0000 | [diff] [blame] | 26 | << ")" << std::endl; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 27 | #endif |
| 28 | } |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | int main(int argc, char** argv) |
| 33 | { |
bart | c45fec3 | 2008-05-01 15:39:18 +0000 | [diff] [blame] | 34 | unsigned thread_count; |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 35 | int i; |
| 36 | |
| 37 | thread_count = argc > 1 ? atoi(argv[1]) : 50; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 38 | 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, |
bart | 91740e2 | 2008-03-11 20:06:50 +0000 | [diff] [blame] | 43 | &thread_count); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 44 | #if 0 |
| 45 | std::cout << "created " << thread_count << "(" << s_thread[thread_count] |
bart | 91740e2 | 2008-03-11 20:06:50 +0000 | [diff] [blame] | 46 | << ")" << std::endl; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 47 | #endif |
sewardj | c68cbe3 | 2007-11-27 01:59:38 +0000 | [diff] [blame] | 48 | for (i = thread_count; i >= 0; i--) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 49 | { |
| 50 | // std::cout << "join " << i << "(" << s_thread[i] << ")" << std::endl; |
| 51 | pthread_join(s_thread[i], 0); |
| 52 | } |
| 53 | return 0; |
| 54 | } |