1 /*
2  * Test program that triggers a race between pthread_barrier_wait() and
3  * pthread_barrier_destroy(): proper synchronization is missing between
4  * the pthread_barrier_wait() and the pthread_barrier_destroy() calls. This
5  * test program is based on the example that was posted on February 5, 2009 by
6  * Christoph Bartoschek on the valgrind-users mailing list. Redistribution of
7  * the source code below is permitted under the GPLv2 license.
8  *
9  * See also http://article.gmane.org/gmane.comp.debugging.valgrind/8945/match=pthread_barrier_wait
10  */
11 
12 
13 #define _GNU_SOURCE
14 
15 #include <pthread.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 
19 static pthread_barrier_t* barrier;
20 
21 
22 static void* thread(void* arg)
23 {
24   pthread_barrier_wait(barrier);
25   return NULL;
26 }
27 
28 int main()
29 {
30   pthread_t tid;
31 
32   barrier = (pthread_barrier_t *) malloc(sizeof(*barrier));
33   pthread_barrier_init(barrier, NULL, 2);
34 
35   pthread_create(&tid, NULL, thread, NULL);
36 
37   pthread_barrier_wait(barrier);
38   /*
39    * The sleep() call below ensures that the pthread_barrier_destroy() call
40    * happens after the created thread has returned from pthread_barrier_wait().
41    */
42   sleep(1);
43   pthread_barrier_destroy(barrier);
44   free(barrier);
45 
46   pthread_join(tid, NULL);
47   return 0;
48 }
49