For this assignment, you will implement a synchronization primitive known as a barrier.
When creating a barrier, you specify a number of threads,
To use the barrier, a thread calls
barrier_wait, which will block the thread until
num_threads threads have arrived at the barrier.
Barriers are a simple and useful synchronization primitive, particularly for scientific computation where some parallel calculation must proceed in phases.
Barriers make it easy to guarantee that all threads have completed one phase of a calculation before moving on to the next phase.
Unlike our previous individual assignments, this assignment does not include start code for a useful program. Instead, you will write the barrier as if it was going into a library for other programs to use. The starter code does include scaffolding so you can use the Google Test framework to test your implementatation.
num_threadsis less than the number of different threads that call
The interface for your barrier is in the file barrier.h. You should implement each of these functions in barrier.c.
void barrier_init(barrier_t* b, size_t num_threads)
bpoints to memory already allocated to hold a barrier. Initialize the barrier so it will block waiters until
void barrier_wait(barrier_t* b)
num_threadsthreads have arrived at the barrier. The barrier can be reused an unlimited number of times, so make sure it is set up to accept new arrivals once waiting threads wake up.
void barrier_destroy(barrier_t* b)
When you implement your barrier, make sure you consider the following issues and requirements:
barrier_waitagain before previously-waiting threads have woken up. Make sure you wake the older threads from the barrier, not the new arrival.
The POSIX threads library on some systems includes a barrier implementation, which you can read about in the Oracle Pthreads Documentation. You are welcome to use other pthreads primitives like mutexes and condition variables when implementing barriers, but you may not use pthreads barriers in your implementation.