#define _XOPEN_SOURCE 600
#include <pthread.h>
#include <stdio.h>
pthread_barrier_t barrier;
#define NUM_THREADS 5
void *thread_routine(void *arg)
{
int thd_id = (int) arg;
int rc;
printf("thd %d: before teh barrier\n", thd_id);
rc = pthread_barrier_wait(&barrier);
if (rc == PTHREAD_BARRIER_SERIAL_THREAD) {
printf("thd %d let the flood in\n", thd_id);
}
printf("thd %d: after the barrier\n", thd_id);
return NULL;
}
int main()
{
int i;
pthread_t tids[NUM_THREADS];
pthread_barrier_init(&barrier, NULL, NUM_THREADS);
for (i = 0; i < NUM_THREADS; i++)
pthread_create(&tids[i], NULL, thread_routine, (void *) i);
for (i = 0; i < NUM_THREADS; i++)
pthread_join(tids[i], NULL);
pthread_barrier_destroy(&barrier);
return 0;
}