Previous | Next --- Slide 42 of 47
Back to Lecture Thumbnails
Claire

Are barriers implemented with a condition variables? Like could a barrier be a condition variable where threads would have to wait at the condition variable until all threads have arrived in order to work on the next dependent part of the problem?

nickbowman

@Claire I think there's lots of different possible ways that barriers could be implemented, but to me the most natural one might be a semaphore (although in the end the semaphore is likely implemented on top of a condition variable) where all participating threads have to up the semaphore before any of them are allowed to proceed.

a7hu

The most basic way of implementing a barrier is using mainly two variables, say "arrive_counter" and "leave_counter". When a thread arrives at a barrier, the "arrive_counter" increments by 1. When a thread exits a barrier, the "leave_counter" decrements by 1. A thread is only allowed to exit the barrier only when arriver_counter equals the number of threads to block on the barrier. When "leave_counter hits 0, i.e. all the threads exit the barrier, the state of the barrier is reset.

Please log in to leave a comment.