blob: 2255a0e71e55931d220d1c61f9c1aada3d6e6084 [file] [log] [blame]
bartb235a672009-02-22 09:26:22 +00001/*
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
bart3355ec52009-04-17 17:51:31 +000013#define _GNU_SOURCE
14
bartb235a672009-02-22 09:26:22 +000015#include <pthread.h>
16#include <stdlib.h>
njnefc13c22009-02-23 06:44:51 +000017#include <unistd.h>
bartb235a672009-02-22 09:26:22 +000018
19static pthread_barrier_t* barrier;
20
21
22static void* thread(void* arg)
23{
24 pthread_barrier_wait(barrier);
25 return NULL;
26}
27
28int 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}