
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Library - <semaphore>
The <semaphore> header in C++20, is used to control access to a shared resource by multiple threads. It is used in scenarios where to limit the number of threads that can access a section or shared resources simultaneously.
The <semaphore> can be in two states; binary semaphore or counting semaphore. Where a binary_semaphore allows a single thread to access a resource at a time, while counting_semaphore allows up to a certain number of threads. When the count reaches zero, the additional threads becomes blocked until the count gets greater than zero again.
The counting_semaphore holds an integer value, while binary_semaphore is limited to either 0 or 1
Including <semaphore> Header
To include the <semaphore> header in your C++ program, you can use the following syntax.
#include <semaphore>
Functions of <semaphore> Header
Below is list of all functions from <semaphore> header.
Sr.No | Functions & Description |
---|---|
1 |
release
It increments the internal counter and unblocks acquirers. |
2 |
acquire
It decrements the internal counter or blocks until it can. |
3 |
try_acquire
It tries to decrement the internal counter without blocking. |
4 |
try_acquire_for
It tries to decrement the internal counter, blocking for up to a duration time. |
5 |
try_acquire_until
It tries to decrement the internal counter, blocking until a point in time. |
5 |
max
It returns the maximum possible value of the internal counter. |
Controlling the Access
In the following example, we are going to use the counting semaphore to limit access to a shared resource to a maximum of two threads at a time.
#include <iostream> #include <semaphore> #include <thread> std::counting_semaphore < 2 > sem(2); void a(int b) { sem.acquire(); std::cout << "A" << b << " Accessing Resource.\n"; std::this_thread::sleep_for(std::chrono::seconds(1)); std::cout << "A" << b << " Releasing Resource.\n"; sem.release(); } int main() { std::thread x1(a, 1); std::thread x2(a, 2); std::thread x3(a, 3); x1.join(); x2.join(); x3.join(); return 0; }
Output
Output of the above code is as follows −
A1 Accessing Resource. A2 Accessing Resource. A1 Releasing Resource. A3 Accessing Resource. A2 Releasing Resource. A3 Releasing Resource.