bart | b235a67 | 2009-02-22 09:26:22 +0000 | [diff] [blame] | 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 | #include <pthread.h> |
| 14 | #include <stdlib.h> |
njn | efc13c2 | 2009-02-23 06:44:51 +0000 | [diff] [blame^] | 15 | #include <unistd.h> |
bart | b235a67 | 2009-02-22 09:26:22 +0000 | [diff] [blame] | 16 | |
| 17 | static pthread_barrier_t* barrier; |
| 18 | |
| 19 | |
| 20 | static void* thread(void* arg) |
| 21 | { |
| 22 | pthread_barrier_wait(barrier); |
| 23 | return NULL; |
| 24 | } |
| 25 | |
| 26 | int main() |
| 27 | { |
| 28 | pthread_t tid; |
| 29 | |
| 30 | barrier = (pthread_barrier_t *) malloc(sizeof(*barrier)); |
| 31 | pthread_barrier_init(barrier, NULL, 2); |
| 32 | |
| 33 | pthread_create(&tid, NULL, thread, NULL); |
| 34 | |
| 35 | pthread_barrier_wait(barrier); |
| 36 | /* |
| 37 | * The sleep() call below ensures that the pthread_barrier_destroy() call |
| 38 | * happens after the created thread has returned from pthread_barrier_wait(). |
| 39 | */ |
| 40 | sleep(1); |
| 41 | pthread_barrier_destroy(barrier); |
| 42 | free(barrier); |
| 43 | |
| 44 | pthread_join(tid, NULL); |
| 45 | return 0; |
| 46 | } |