see the following:
/*
- barrier1.c
*/
#include <stdio.h>
#include <time.h>
#include <pthread.h>
#include <sys/neutrino.h>
pthread_barrier_t barrier; // the barrier synchronization object
void *
thread2 (void *not_used)
{
time_t now;
time (&now);
printf (“thread1 starting at %s”, ctime (&now));
// do the computation
// let’s just do a sleep here…
sleep (5);
pthread_barrier_wait (&barrier);
// after this point, all three threads have completed.
time (&now);
printf (“barrier in thread1() done at %s”, ctime (&now));
}
void *
thread1 (void *not_used)
{
time_t now;
time (&now);
printf (“thread2 starting at %s”, ctime (&now));
// do the computation
// let’s just do a sleep here…
sleep (4);
pthread_barrier_wait (&barrier);
// after this point, all three threads have completed.
time (&now);
printf (“barrier in thread2() done at %s”, ctime (&now));
}
main () // ignore arguments
{
time_t now;
// create a barrier object with a count of 3
pthread_barrier_init (&barrier, NULL, 3);
// start up two threads, thread1 and thread2
pthread_create (NULL, NULL, thread1, NULL);
pthread_create (NULL, NULL, thread2, NULL);
// at this point, thread1 and thread2 are running
// now wait for completion
time (&now);
printf (“main() waiting for barrier at %s”, ctime (&now));
pthread_barrier_wait (&barrier);
// after this point, all three threads have completed.
time (&now);
printf (“barrier in main() done at %s”, ctime (&now));
sleep(1);
}
the output:
thread1 starting at Sun Dec 24 04:54:10 2000
main() waiting for barrier at Sun Dec 24 04:54:10 2000
thread2 starting at Sun Dec 24 12:54:10 2000
barrier in thread1() done at Sun Dec 24 12:54:15 2000
barrier in main() done at Sun Dec 24 12:54:15 2000
barrier in thread2() done at Sun Dec 24 12:54:15 2000