Sujet : Re: counting_semaphore question
De : ben (at) *nospam* bsb.me.uk (Ben Bacarisse)
Groupes : comp.lang.c++Date : 12. Sep 2024, 12:25:07
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <87ikv1i2yk.fsf@bsb.me.uk>
References : 1 2 3 4 5
User-Agent : Gnus/5.13 (Gnus v5.13)
Muttley@dastardlyhq.com writes:
On Wed, 11 Sep 2024 14:10:05 +0200
Bonita Montero <Bonita.Montero@gmail.com> boringly babbled:
Am 11.09.2024 um 14:03 schrieb Muttley@dastardlyhq.com:
>
What upper limit? The max number of threads allowed in the protected section
above is 2, not 10. Or do you mean it'll only count up to 10 threads waiting
and ignore any beyond that? What happens if thread 11 comes along?
>
The static parmeter is the maximum counter and the the two is the
initial counter. So two threads can currently acquire that semaphore.
>
Sorry, I still don't get it. What do you mean by maximum counter?
You might want to consider who you are talking to.
Let's take std::counting_semaphore<M> sem(N); as an example. The N is
the important number but it is not a 'maximum' in any hard sense -- it
is simply the initial value of the counter. If it were some sort of
hard maximum, std::counting_semaphore sem(0); would be useless but it
isn't -- it simply means that at least one release() is needed before an
acquire() won't block. Of course, if often /does/ represent the
maximum number of threads that can access some resource because the
usual pattern is to use std::counting_semaphore sem(N) with the standard
pattern of every thread calling acquire() and then release().
The template parameter M is very different and rather unusual. It is,
first and foremost, just a hint to the implementation about how much
space will be needed for the counter, so the most useful value for M is
1 because some systems can implement binary semaphores more efficiently
than counting ones.
But this "limit" is not enforced other than by making the behaviour
undefined when it is exceeded. Pre-conditions on the operations state
that the counter must always be >= 0 and <= M, but you won't be able to
check this unless the implementation decides to enforce these
pre-conditions with some sort of error report.
-- Ben.