This material is part of the course Practical Introduction to Parallel Programming and meant for educational purposes only.
Task
Implement a struct barrier_t
that can be used to synchronise the execution of a group of threads.
That is, this data type should enable multiple threads to wait until they all have reached a certain point in the execution of a parallel program.
Hints
You must implement the following functions: barrier_init
, barrier_destroy
, and barrier_wait
.
/* bar INOUT A pointer to the barrier object to be initialised. */ /* nt IN The number of threads that will participate in a barrier operation. */ void barrier_init ( barrier_t* bar, int nt );
The function barrier_init
initialises the members of the struct barrier_t
. It must be called before a barrier operation can be performed.
The parameter nt
specifies the number of threads that will participate in a barrier operation. This will be the number of threads that should call the function barrier_wait
.
/* bar INOUT A pointer to the barrier object to be destroyed. */ void barrier_destroy ( barrier_t* bar );
The function barrier_destroy
destroys a barrier object.
It must be called when a barrier object is no longer needed. Each call to barrier_init
must be paired with a call to barrier_destroy
.
/* bar IN A pointer to a barrier object. */ void barrier_wait ( barrier_t* bar );
The function barrier_wait
blocks all calling threads until the last thread in the group has entered this function.
To be precise, this function blocks until it has been called by the number of threads indicated by the nt
parameter of the barrier_init
function.
The struct barrier_t
must contain at least a mutex, a condition variable and a counter that keeps track of the number of threads that have called the function barrier_wait
.
You need more members; that is for you to figure out.
Test the program by spawning multiple threads and by performing at least four barrier operations in succession.