Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #120119

Re: counting_semaphore question

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c++
Subject Re: counting_semaphore question
Date 2024-09-17 09:22 +0200
Organization A noiseless patient Spider
Message-ID <vcbant$3cto1$1@dont-email.me> (permalink)
References (2 earlier) <vbs0tm$3jf62$1@dont-email.me> <vbs1al$3jgnp$1@raubtier-asyl.eternal-september.org> <vbs2fm$3jp23$1@dont-email.me> <vbs95s$3l7ms$1@dont-email.me> <vca7hs$32c6u$1@dont-email.me>

Show all headers | View raw


On 16/09/2024 23:22, Chris M. Thomasson wrote:
> On 9/11/2024 7:23 AM, David Brown wrote:
>> On 11/09/2024 14:29, Muttley@dastardlyhq.com wrote:
>>> 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?
>>>
>>> Maximum threads that can wait on the semaphore and 11 gets an error?
>>> Maximum threads that can ever access the protected section and after 
>>> 10 have
>>> accessed it the section is blocked to all threads?
>>>
>>
>> You are talking about a counting semaphore here - the number will be 
>> the maximum value of the count.  (The actual maximum value could be 
>> bigger than the template parameter, but not smaller than it.)  If this 
>> maximum value is 10, then you can acquire the semaphore 10 times 
>> without blocking - any future acquires will block until there are 
>> releases.
>>
>> Semaphores can be acquired and released by any threads - one thread 
>> can acquire often, another thread can release them, or you can have 
>> any combination.  They are not used for protection sections or data - 
>> they are used as signalling mechanisms.  One or more "producer" thread 
>> might make objects, and one or more "consumer" threads used them, in 
>> which case your 10 here is the maximum number of these objects that 
>> will be buffered up at a time.
> 
> [...]
> 
> Think of creating a queue with 100,000 elements in it. The semaphore to 
> protect this queue should have an initial state of 100,000. The least 
> min value, should be high because the queue might have 1.9 million 
> elements in there. So, for this queue:
> 
> std::counting_semaphore<PTRDIFF_MAX> sem(100000);
> 
> right? There are 100000 elements in the queue because its initialized 
> that way. A single thread can pop and work on 100000 elements before it 
> blocks on a queue empty condition.

Roughly yes - assuming you are setting up the queue with the first 
100,000 elements and then creating the semaphore, then allowing the 
queue to grow or shrink unhindered.  It's more common for queues to 
start with a semaphore of count 0 (an empty queue) and then add 
elements, but if you have 100,000 elements in advance then this would be 
a more efficient initialisation.

You can also omit the template parameter, in which case an 
implementation-defined default is used.  A quick test on godbolt's gcc 
shows that this gives a max() of 0x7fff'ffff, and that this is the 
largest value supported by the C++ library used by that implementation - 
it keeps the count as a signed 32-bit integer.  Attempting to use 
PTRDIFF_MAX triggers a static assertion:

	static_assert(__least_max_value <= __semaphore_impl::_S_max);

I don't know if this is a conformance flaw in the C++ standard library, 
or if I just haven't read the documentation details well enough.  And I 
couldn't see any specification for what the default value should be, but 
I can't think of any other reasonable choice than the biggest acceptable 
value.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

counting_semaphore question Muttley@dastardlyhq.com - 2024-09-11 10:15 +0000
  Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-11 13:48 +0200
    Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-11 12:03 +0000
      Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-11 14:10 +0200
        Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-11 12:29 +0000
          Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-11 15:54 +0200
            Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-11 14:12 +0000
          Re: counting_semaphore question David Brown <david.brown@hesbynett.no> - 2024-09-11 16:23 +0200
            Re: counting_semaphore question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-16 14:22 -0700
              Re: counting_semaphore question David Brown <david.brown@hesbynett.no> - 2024-09-17 09:22 +0200
                Re: counting_semaphore question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-17 11:56 -0700
                Re: counting_semaphore question David Brown <david.brown@hesbynett.no> - 2024-09-18 09:32 +0200
                Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-19 12:57 +0200
                Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-19 13:01 +0200
                Re: counting_semaphore question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-19 12:09 -0700
                Re: counting_semaphore question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-19 13:27 -0700
                Re: counting_semaphore question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-19 13:30 -0700
          Re: counting_semaphore question Ben Bacarisse <ben@bsb.me.uk> - 2024-09-12 11:25 +0100
            Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-12 11:00 +0000
              Re: counting_semaphore question Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-09-12 07:34 -0700
                Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-12 14:53 +0000
                Re: counting_semaphore question David Brown <david.brown@hesbynett.no> - 2024-09-13 08:39 +0200
                Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-13 07:26 +0000
                Re: counting_semaphore question David Brown <david.brown@hesbynett.no> - 2024-09-13 12:54 +0200
                Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-13 13:21 +0000
                Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-13 15:29 +0200
                Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-13 13:41 +0000
                Re: counting_semaphore question David Brown <david.brown@hesbynett.no> - 2024-09-15 17:33 +0200
                Re: counting_semaphore question David Brown <david.brown@hesbynett.no> - 2024-09-13 16:46 +0200
                Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-13 14:59 +0000
                Re: counting_semaphore question Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-09-13 07:43 -0700
        Re: counting_semaphore question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-11 13:51 -0700
          Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-12 04:21 +0200
            Re: counting_semaphore question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-12 11:57 -0700
              Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-13 14:29 +0200
                Re: counting_semaphore question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-13 12:44 -0700
                Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-14 01:43 +0200
                Re: counting_semaphore question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-13 22:21 -0700
                Re: counting_semaphore question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-14 12:07 -0700
                Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-14 09:18 +0000
                Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-14 11:40 +0200
                Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-14 10:58 +0000
                Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-14 13:15 +0200
                Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-14 15:13 +0000
                Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-14 17:26 +0200
                Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-14 15:48 +0000
                Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-14 17:50 +0200
                Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-14 15:55 +0000
                Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-14 17:57 +0200
                Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-15 09:05 +0000
                Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-15 11:48 +0200
                Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-15 09:52 +0000
                Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-15 14:50 +0200
                Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-15 15:28 +0000
                Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-16 05:02 +0200
                Re: counting_semaphore question scott@slp53.sl.home (Scott Lurndal) - 2024-09-15 16:27 +0000
                Re: counting_semaphore question scott@slp53.sl.home (Scott Lurndal) - 2024-09-15 16:26 +0000
                Re: counting_semaphore question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-14 12:12 -0700
                Re: counting_semaphore question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-14 17:21 -0700
                Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-15 07:11 +0200
                Re: counting_semaphore question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-14 22:20 -0700
                Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-15 09:35 +0200
                Re: counting_semaphore question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-15 12:59 -0700
                Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-16 05:04 +0200
                Re: counting_semaphore question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-16 13:30 -0700
                Re: counting_semaphore question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-14 12:09 -0700
  Re: counting_semaphore question Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-09-11 07:37 -0700
    Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-11 15:03 +0000
      Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-11 17:27 +0200
        Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-11 16:04 +0000
          Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-11 18:16 +0200
            Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-11 18:16 +0000
              Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-12 04:20 +0200
          Re: counting_semaphore question "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2024-09-12 13:00 -0700
      Re: counting_semaphore question Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-09-11 20:56 -0700
        Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-12 07:43 +0000
          Re: counting_semaphore question David Brown <david.brown@hesbynett.no> - 2024-09-12 13:30 +0200
            Re: counting_semaphore question Muttley@dastardlyhq.com - 2024-09-12 14:48 +0000
          Re: counting_semaphore question Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-12 15:38 +0200
          Re: counting_semaphore question Andrey Tarasevich <andreytarasevich@hotmail.com> - 2024-09-12 07:05 -0700

csiph-web