![]() |
The C++20 <semaphore> header is part of the Concurrency Library Technical Specification (TS). Semaphores are synchronization primitives that help control access to shared resources in multi-threaded programs. The <semaphore> header provides the standard C++ way to work with semaphores. In this article, we have covered important sections of semaphore headers such as the main classes, and usage of semaphore headers in C++20 along with examples. How to use <semaphore> in C++20?Below is the step-by-step tutorial on the use of semaphores in C++ programs. STEP 1: Include the HeaderTo use semaphores in your C++ program, you need to include the <semaphore> header: #include <semaphore> STEP 2: Semaphore BasicsYou can create a semaphore object like this: std::counting_semaphore<size_t> sem(1); // Initialize a semaphore with an initial count of 1 std::counting_semaphore is a type of semaphore that allows a specified number of threads to access a resource concurrently. In this example, only one thread can access the resource protected by sem at a time. STEP 3: Acquiring and ReleasingThere are 3 methods to acquire and release a semaphore object: Method 1: Aquire and Release To acquire (lock) the semaphore, you can use the acquire method: sem.acquire(); // Critical section code sem.release(); The acquire method decreases the semaphore count by one, effectively locking it. The release method increases the count, releasing the semaphore. Method 2: Try-Aquire You can also use the try_acquire method to try to acquire the semaphore without blocking: if (sem.try_acquire()) { // Successfully acquired the semaphore // Critical section code sem.release(); } else { // Semaphore was not acquired } Method 2: Waiting with TimeoutC++20 also introduced the try_acquire_for and try_acquire_until methods to try acquiring the semaphore with a timeout. if (sem.try_acquire_for(std::chrono::seconds(1))) { // Successfully acquired the semaphore within 1 second // Critical section code sem.release(); } else { // Semaphore was not acquired within 1 second } Types of SemaphoresThe <semaphores> header provides two types of semaphores that are: 1. std::counting_semaphoreA counting semaphore is a synchronization primitive that allows multiple threads to access a shared resource up to a certain limit.
Example C++
Output Thread 2 acquired the semaphore. Thread 2 released the semaphore. Thread 1 acquired the semaphore. Thread 1 released the semaphore. Thread 3 acquired the semaphore. Thread 3 released the semaphore. 2. std::binary_semaphoreA binary semaphore is a simpler version of a semaphore that can have only two values: 0 and 1.
Example C++
Output Thread 1 acquired the semaphore. Thread 1 released the semaphore. Thread 2 acquired the semaphore. Thread 2 released the semaphore. Advantages of SemaphoresThe following are some main advantages of using semaphores over similar constructs:
|
Reffered: https://www.geeksforgeeks.org
C++ |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |