General Rendezvous with Semaphores

Viewed 2690

I'm working through the The Little Book of Semaphores on implementing a barrier for rendezvous. Does the following code work as a barrier?

If we have the following variables:

n = the number of threads
barrier = Semaphore(-n + 1)

And we execute n threads on the following code.

# Rendezvous
barrier.signal()
barrier.wait()
barrier.signal()

# critical point

Book Answer

Here's the author's variables.

n = the number of threads
count = 0
mutex = Semaphore(1)
barrier = Semaphore(0)

Author's barrier solution

mutex.wait()
count = count + 1
mutex.signal()

if count == n: barrier.signal()

barrier.wait()
barrier.signal()

# critical point

I understand the author's solution, I want to know if my approach works.

1 Answers
Related