Reputation: 3468
I learned about synchronization in class recently, and I'm a little confused about the difference between signal and broadcast. I know for signal, when it happens it wakes up the first thread in its waitlist. That thread will claim the lock after the signal thread unlocks. Then what happens to broadcast? when broadcast is called all the waiting threads are woken up. Then when the broadcast thread unlocks, which of these threads get to take that lock?
Upvotes: 1
Views: 187
Reputation: 279255
All the threads are unblocked. All of them try to acquire the lock. Whichever one succeeds first returns from its wait
function holding the lock. When that thread later releases the lock, one of the threads still trying to acquire it, will get it.
In practice I suspect that on a broadcast the OS will move the waitlist directly across and add it to the list of threads waiting to acquire the lock (accounting for priority if it orders such lists by priority). But that's an implementation detail.
Upvotes: 1