blob: c91be67173a98dcb8f0fb2c04d7dcb7f9a4894e3 [file] [log] [blame]
bart55df6e72009-02-16 19:42:17 +00001/**
2 * Test whether detached threads are handled properly.
3 * This test program is based on pth_detached.c, with the difference that
4 * in this test program the main thread uses a counting semaphore instead
5 * of a counter protected by a mutex to wait until all detached threads
6 * finished.
bart5ad46982008-03-16 07:45:44 +00007 */
8
9
10#include <assert.h>
11#include <pthread.h>
12#include <semaphore.h>
13#include <stdio.h>
14#include <stdlib.h>
15#include <unistd.h>
bart5ad46982008-03-16 07:45:44 +000016
17
bart5ad46982008-03-16 07:45:44 +000018static sem_t s_sem;
19
20
bart5ad46982008-03-16 07:45:44 +000021static void increment_finished_count()
22{
23 sem_post(&s_sem);
24}
25
26static void* thread_func1(void* arg)
27{
bart5ad46982008-03-16 07:45:44 +000028 write(STDOUT_FILENO, ".", 1);
29 increment_finished_count();
30 return 0;
31}
32
33static void* thread_func2(void* arg)
34{
bart5ad46982008-03-16 07:45:44 +000035 pthread_detach(pthread_self());
36 write(STDOUT_FILENO, ".", 1);
37 increment_finished_count();
38 return 0;
39}
40
41int main(int argc, char** argv)
42{
43 const int count1 = argc > 1 ? atoi(argv[1]) : 100;
44 const int count2 = argc > 2 ? atoi(argv[2]) : 100;
bart5ad46982008-03-16 07:45:44 +000045 int thread_arg[count1 > count2 ? count1 : count2];
46 int i;
47 int detachstate;
48 pthread_attr_t attr;
49
bart5ad46982008-03-16 07:45:44 +000050 for (i = 0; i < count1 || i < count2; i++)
51 thread_arg[i] = i;
52
53 sem_init(&s_sem, 0, 0);
bart31b983d2010-02-21 14:52:59 +000054
bart5ad46982008-03-16 07:45:44 +000055 pthread_attr_init(&attr);
56 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
57 assert(pthread_attr_getdetachstate(&attr, &detachstate) == 0);
58 assert(detachstate == PTHREAD_CREATE_DETACHED);
59 pthread_attr_setstacksize(&attr, 16384);
bart31b983d2010-02-21 14:52:59 +000060 // Create count1 detached threads by setting the "detached" property via
bart5ad46982008-03-16 07:45:44 +000061 // thread attributes.
62 for (i = 0; i < count1; i++)
63 {
64 pthread_t thread;
65 pthread_create(&thread, &attr, thread_func1, &thread_arg[i]);
66 }
67 // Create count2 detached threads by letting the threads detach themselves.
68 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
69 assert(pthread_attr_getdetachstate(&attr, &detachstate) == 0);
70 assert(detachstate == PTHREAD_CREATE_JOINABLE);
71 for (i = 0; i < count2; i++)
72 {
73 pthread_t thread;
74 pthread_create(&thread, &attr, thread_func2, &thread_arg[i]);
75 }
76 pthread_attr_destroy(&attr);
77
78 // Wait until all detached threads have written their output to stdout.
79 for (i = 0; i < count1 + count2; i++)
80 {
81 sem_wait(&s_sem);
82 }
83
84 write(STDOUT_FILENO, "\n", 1);
85
86 sem_destroy(&s_sem);
87
88 return 0;
89}