What's the difference between binary_semaphore and counting_semaphore? I look into the std header. The least_max_value is only used in max() which doesn't really do anything. So they are just the same thing?!
If you are going strictly by cppreference documentation, then make sure to go through the whole of it. And if you seek answers maybe you should learn to read standard library code as well.
Cppreference mentions the following:
LeastMaxValue is the minimum max value, not the actual max value.[...]
A counting_semaphore contains an internal counter initialized by the constructor.[...]
Going through the code here, confirms the same.
I am just pasting snippets relevant to the discussion here.
template<ptrdiff_t __least_max_value = __semaphore_impl::_S_max>
class counting_semaphore{
static_assert(__least_max_value >= 0);
static_assert(__least_max_value <= __semaphore_impl::_S_max);
__semaphore_impl _M_sem;
public:
explicit counting_semaphore(ptrdiff_t __desired) noexcept
: _M_sem(__desired)
It is __desired which is used and not least_max_val for further decisions as was mentioned in the Cppreference documentation. The _least_max_val is used only in the static assertions so that a compiler error is issued when you pass in a counter value larger than what can be accomodated by the semaphore implementation on that specific platform (this is usually done to ensure lock freeness. If atomic exchange operations and such require a lock on a integer type that is required to fit in the counter value, then this particular implementation does not allow it).
Now _M_sem which is of type `semaphore_impl `(an alias for __atomic_semaphore) is defined here :
https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/bits/semaphore_base.h#L185
The constructor of this struct is passed in the value of __desired from counting semaphore. This value is then used in all acquire and release operations thereafter.
Anonymous
Richard Luo 🐱
klimi
Arvin
صغير
Ammar
Jose
Aman😒
Marco
Chat Boss
Aysel
m²
Suraj
Mahmoud
itsmanjeet