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
12 #include <semaphore.h>
21 static void increment_finished_count()
26 static void* thread_func1(void* arg
)
28 write(STDOUT_FILENO
, ".", 1);
29 increment_finished_count();
33 static void* thread_func2(void* arg
)
35 pthread_detach(pthread_self());
36 write(STDOUT_FILENO
, ".", 1);
37 increment_finished_count();
41 int main(int argc
, char** argv
)
43 const int count1
= argc
> 1 ? atoi(argv
[1]) : 100;
44 const int count2
= argc
> 2 ? atoi(argv
[2]) : 100;
45 int thread_arg
[count1
> count2
? count1
: count2
];
50 for (i
= 0; i
< count1
|| i
< count2
; i
++)
53 sem_init(&s_sem
, 0, 0);
55 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);
60 // Create count1 detached threads by setting the "detached" property via
62 for (i
= 0; i
< count1
; i
++)
65 pthread_create(&thread
, &attr
, thread_func1
, &thread_arg
[i
]);
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
++)
74 pthread_create(&thread
, &attr
, thread_func2
, &thread_arg
[i
]);
76 pthread_attr_destroy(&attr
);
78 // Wait until all detached threads have written their output to stdout.
79 for (i
= 0; i
< count1
+ count2
; i
++)
84 write(STDOUT_FILENO
, "\n", 1);
85 fprintf(stderr
, "Done.\n");