Deutsch English Français Italiano |
<87ikv1i2yk.fsf@bsb.me.uk> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.nobody.at!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Ben Bacarisse <ben@bsb.me.uk> Newsgroups: comp.lang.c++ Subject: Re: counting_semaphore question Date: Thu, 12 Sep 2024 11:25:07 +0100 Organization: A noiseless patient Spider Lines: 41 Message-ID: <87ikv1i2yk.fsf@bsb.me.uk> References: <vbrqjj$3hvsd$1@dont-email.me> <vbs01c$3j5mc$1@raubtier-asyl.eternal-september.org> <vbs0tm$3jf62$1@dont-email.me> <vbs1al$3jgnp$1@raubtier-asyl.eternal-september.org> <vbs2fm$3jp23$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Thu, 12 Sep 2024 12:25:09 +0200 (CEST) Injection-Info: dont-email.me; posting-host="9b7858c5e222e20447475885d34c68cc"; logging-data="233949"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18FCzL0h/UxE7bqM57YZU6RXqbVbHxbXoI=" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:1X8VGi0JqDw6axvcaLrj+IgDCD0= sha1:Rxakg5g8U0R+MlnwrAYPkKIkg3A= X-BSB-Auth: 1.68449e86838cda201b94.20240912112507BST.87ikv1i2yk.fsf@bsb.me.uk Bytes: 3139 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.