ragingcorgi
ragingcorgi

Reputation: 3468

synchronization, lock passing with broadcast

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

Answers (1)

Steve Jessop
Steve Jessop

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

Related Questions