I read The Little Book of Semaphores, and on page 41 there is a solution to the reusable barrier problem. The problem is why it will not create a deadlock.
1 # rendezvous 2 3 mutex.wait() 4 count += 1 5 if count == n: 6 turnstile2.wait() # lock the second 7 turnstile.signal() # unlock the first 8 mutex.signal() 9 10 turnstile.wait() # first turnstile 11 turnstile.signal() 12 13 # critical point 14 15 mutex.wait() 16 count -= 1 17 if count == 0: 18 turnstile.wait() # lock the first 19 turnstile2.signal() # unlock the second 20 mutex.signal() 21 22 turnstile2.wait() # second turnstile 23 turnstile2.signal()
In this solution, between lines 15 and 20, is it not a bad habit to call wait () on the semaphore (on line 18) while holding the mutex that causes the deadlock? Please explain. Thank.
mutex count. , (if count == n) (. ) (n-1) 10). .
mutex
count
if count == n
, count ( mutext ). turnstile entring (. ) (n-1) , 22. .
turnstile
, turnstile , turnstile2 .
turnstile2
: , () 18, turnstile , ( 22). turnstile2