Semaphores
C++20 introduces new synchronization primitives to write multithreaded applications. In this section, we will look at semaphores.
A semaphore is a counter that manages the number of permits available for accessing a shared resource. Semaphores can be classified into two main types:
- A binary semaphore is like a mutex. It has only two states: 0 and 1. Even though a binary semaphore is conceptually like a mutex, there are some differences between a binary semaphore and a mutex that we will see later in this section.
- A counting semaphore can have a value greater than 1 and is used to control access to a resource that has a limited number of instances.
C++20 implements both binary and counting semaphores.
Binary semaphores
A binary semaphore is a synchronization primitive that can be used to control access to a shared resource. It has two states: 0 and 1. A semaphore with a value of 0 indicates that the resource is unavailable, while a semaphore with...