Course contentsShow
← Computer Science
Lesson 1309 of 2,87230. Process SynchronizationFree lesson

Signal vs Broadcast

Compare signal() which wakes one thread versus broadcast() which wakes all waiting threads.

Signal vs Broadcast

You'll learn the difference between waking one waiting thread versus waking all waiting threads with condition variables.

The Core Difference

When threads are waiting on a condition variable, you have two ways to wake them up:

  • signal() wakes up one waiting thread (if any are waiting)
  • broadcast() wakes up all waiting threads

Think of it like a teacher in a classroom: signaling is tapping one student on the shoulder to answer a question, while broadcasting is announcing "Everyone, wake up!" to the whole class.

When to Use Signal

Use signal() when:

  • Only one thread can make progress when the condition becomes true
  • You're releasing a single resource (like one item in a buffer)
  • Waking multiple threads would be wasteful

Example: In a producer-consumer scenario, when you produce one item, you only need to wake one consumer.

When to Use Broadcast

Use broadcast() when:

  • Multiple threads might be able to proceed
  • The condition change affects all waiters differently
  • You're not sure which specific thread should wake up
  • Threads are waiting on different predicates with the same condition variable

Example: If you're shutting down a system and all worker threads need to notice and exit, broadcast ensures none are left sleeping.

The Trade-off

Broadcasting is simpler and safer (you won't accidentally leave a thread sleeping), but it can cause a "thundering herd" where many threads wake up, recheck their condition, and most go right back to sleep—wasting CPU cycles. Signaling is more efficient but requires careful reasoning about your program's logic.

Key Takeaway: Use signal() to wake one thread when only one can proceed; use broadcast() when multiple threads might benefit from rechecking the condition or you need guaranteed notification to all waiters.