The other day, I was in a chat were someone asked about what a semaphore is exactly.

Note: I have not much experience with multiprocess- or multithreaded programming, but read enough about it that I think I can understand the basic principles. If I get something wrong, please get in touch with me!

Basically there are two variants of semaphores: binary and counting ones.

Binary semaphore

A binary semaphores act as a lock.

This concept could be hard to understand. So let's go with a real-life example.

Imagine you have a train station with a single track. How many trains can enter that station at the same time?

Exactly. One.

So … how do you prevent them from crashing into each other? Say, by using a traffic light. Only one light is green at a time.

Now, transfer this image to programming.

The station is a resource we want to protect against parallel access. The trains are our processes or threads trying to access that resource. Entering the station could be a write operation, leaving it a read one. They claim access to the resource by means of the traffic light, which acts as „lock” or semaphore here. You could treat it as a flag.

There's one catch, though. Imagine a scenario, where one train sees, that it gets the green light, but in the time between starting its engine and entering the station, another train gets green light as well. Crash, boom.

Here, you might need to use a mutex. I won't go into the details about that concept here.

Counting semaphore

Now let's try to figure out, what a counting semaphore is.

Imagine you want to go shopping at your grocery store. But there's a grassing pandemic outside. Thus, the store put in place a security guard outside, who's in charge of restricting how many people can enter the store at any given time.

Here, the store is your resource. The guard acts as your semaphore, who counts the amount of operations (enter/leave) on the resource.

How do you know that?

I taught those concepts myself. Especially once you read about deadlocks or resource starvation, things become interesting.

My use cases so far were file or database locking. But I want to dig deeper into distributed computing, where these concepts will play an important role.