Day 20/100 100 Days of Code

Photo by Uday Awal on Unsplash

Day 20/100 100 Days of Code


I spent the session watching more tutorials on concurrency. I watched the whole playlist and I learned how both Mutex and BinarySemaphore can be used and where to use them. I learned about the std::jthread object which can be used to automate the join operation and the std::jthread::request_stop to stop any running threads.

What is the difference between Mutex and Semaphore

  1. Allows only one thread to go through a critical section

  2. A section is locked or unlocked

  3. It pretty much is a locking mechanism


  1. Multiple threads can be used at the same time

  2. It depends on the counter

  3. It is a signaling mechanism

When to use Mutex and when to use Semaphore

To run a single operation on multiple threads, use Mutex. To run multiple operations at the same time synchronized, use Semaphore as signaling can be used to make a thread start doing its operation or wait for a signal to start.