What is Thread Signaling?
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.
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.