Sujet : Re: counting_semaphore question
De : Bonita.Montero (at) *nospam* gmail.com (Bonita Montero)
Groupes : comp.lang.c++Date : 11. Sep 2024, 13:48:04
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vbs01c$3j5mc$1@raubtier-asyl.eternal-september.org>
References : 1
User-Agent : Mozilla Thunderbird
Am 11.09.2024 um 12:15 schrieb
Muttley@dastardlyhq.com:
I've been looking at counting_semaphore and it looks useful but something
that doesn't seem to be properly explained anywhere is the template
parameter value. eg you can do:
std::counting_semaphore sem(2)
which will let a max of 2 threads into the protected block at a time or:
std::counting_semaphore<some number> sem(2)
such as
std::counting_semaphore<10> sem(2)
I don't understand what the '10' will do. ..
The ten gives an upper limit beyond the semaphore wont't increment.
Usually you won't need a C++20 semaphore yourself. For most purpose
the mutex and the condition_variable is sufficient.
But if you will build your own synchronization-primitives you inter-
nally would also need a semaphore. The semaphore class itself is
usually built on top of the system-provided futex. I implemented
a reader's writer lock with configurable priority for either rea-
ders or writers (std::shared_mutex only supports reader-priority)
and therefore I needed C++20's counting_semaphore and binary_sema-
phore (which is an template-alias for counting_senaphore<1>).