What are semaphores generalize locks?

Semaphores are like shared counters that help people take turns using something special, just like locks do, but for more than one person at a time.

Imagine you and your friend both want to use the same toy. If there’s only one toy, you need a lock, like a turnstile: when one of you goes through, the other has to wait until it comes back. That's simple, but what if there are two toys, and four friends wanting to play?

That’s where semaphores come in! Think of them as a kind of shared counter with numbers on it. If the number is high enough, more people can go in at once. For example, if the semaphore shows 2, two kids can use the toys without waiting, like having two turnstiles working together.

Now imagine the toy area has three turnstiles: that’s like a semaphore with a value of 3. More friends can play at the same time. When someone is done, they add one to the counter, letting the next person in. This way, semaphores let more people share things fairly, just like locks, but for bigger groups!

Take the quiz →

Examples

  1. A group of kids taking turns using a single toy, but with multiple toys and rules for who goes next.
  2. Imagine a traffic light that lets more than one car go at the same time.
  3. Using a shared ladder where only two people can climb up at once.

Ask a question

See also

Discussion

Recent activity