Synchronization PatternsThread Signaling & CoordinationEasy⏱️ ~2 min

What is Thread Signaling?

Definition
Thread signaling is when one thread sends a message to another thread saying "I am done, you can proceed." It guarantees execution order between threads.

The Core Problem

Thread A reads data from a file. Thread B processes that data. B cannot start until A finishes reading. How does B know when A is done? Without signaling, B would have to keep checking - wasting CPU cycles spinning in a loop.

The Restaurant Kitchen Analogy

Think of a kitchen with a chef and a server. The chef prepares the dish, then rings a bell. The server waits for the bell before taking the plate to the customer. The bell is the signal. Without it, the server would have to keep peeking into the kitchen.

Basic Signaling Pattern
Thread AThread BDo workwait()signal()Proceed!

How It Works With Semaphores

Initialize a semaphore to 0. Thread B calls wait() - since the value is 0, it blocks. Thread A does its work, then calls signal(). This increments the semaphore to 1 and wakes B. B proceeds.

What if A signals first? The semaphore becomes 1. When B later calls wait(), it immediately decrements to 0 and proceeds. No blocking needed. The pattern works regardless of which thread runs first.

Key Insight: Signaling solves the serialization problem - guaranteeing that event A happens before event B, even across threads. It is the fundamental building block for all thread coordination.
💡 Key Takeaways
Signaling lets one thread tell another "I am done." The waiting thread blocks until it receives the signal.
A semaphore initialized to 0 is perfect for signaling. wait() blocks, signal() unblocks.
Order does not matter. If the signal arrives before the wait, the semaphore remembers it. No signal is lost.
Signaling solves serialization - guaranteeing one operation completes before another starts.
This is the foundation for all coordination patterns. Producer-consumer, barriers, and rendezvous all build on signaling.
📌 Examples
1File processing: Reader thread signals writer thread when data is ready. Writer blocks until the signal arrives.
2Initialization: Main thread waits for worker thread to finish setup before proceeding with the application.
3Pipeline stages: Stage 1 signals stage 2, stage 2 signals stage 3. Each stage waits for its predecessor.
← Back to Thread Signaling & Coordination Overview