drd: Add a consistency check
[valgrind.git] / drd / tests / pth_detached_sem.c
blob456d7e85d0367fb81ce2f90ae4df01297d66edf8
1 /**
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.
7 */
10 #include <assert.h>
11 #include <pthread.h>
12 #include <semaphore.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
18 static sem_t s_sem;
21 static void increment_finished_count()
23 sem_post(&s_sem);
26 static void* thread_func1(void* arg)
28 write(STDOUT_FILENO, ".", 1);
29 increment_finished_count();
30 return 0;
33 static void* thread_func2(void* arg)
35 pthread_detach(pthread_self());
36 write(STDOUT_FILENO, ".", 1);
37 increment_finished_count();
38 return 0;
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];
46 int i;
47 int detachstate;
48 pthread_attr_t attr;
50 for (i = 0; i < count1 || i < count2; i++)
51 thread_arg[i] = 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
61 // thread attributes.
62 for (i = 0; i < count1; i++)
64 pthread_t thread;
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++)
73 pthread_t thread;
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++)
81 sem_wait(&s_sem);
84 write(STDOUT_FILENO, "\n", 1);
85 fprintf(stderr, "Done.\n");
87 sem_destroy(&s_sem);
89 return 0;