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


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

Re: counting_semaphore question

From "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Newsgroups comp.lang.c++
Subject Re: counting_semaphore question
Date 2024-09-12 13:00 -0700
Organization A noiseless patient Spider
Message-ID <vbvh97$d9ib$1@dont-email.me> (permalink)
References <vbrqjj$3hvsd$1@dont-email.me> <vbs9uq$3l4l1$1@dont-email.me> <vbsbfu$3lp1e$1@dont-email.me> <vbscsd$3ltfs$1@raubtier-asyl.eternal-september.org> <vbsf2r$3miho$1@dont-email.me>

Show all headers | View raw


On 9/11/2024 9:04 AM, Muttley@dastardlyhq.com wrote:
> On Wed, 11 Sep 2024 17:27:17 +0200
> Bonita Montero <Bonita.Montero@gmail.com> boringly babbled:
>> Am 11.09.2024 um 17:03 schrieb Muttley@dastardlyhq.com:
>>> On Wed, 11 Sep 2024 07:37:13 -0700
>>> Andrey Tarasevich <andreytarasevich@hotmail.com> boringly babbled:
>>>> On 09/11/24 3:15 AM, Muttley@dastardlyhq.com wrote:
>>>>> 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:
>>>>
>>>> No, there's nothing "max" about that 2 here. 2 means that the semaphore
>>>> will be created with 2 available "permissions to enter" (permissions to
>>>> `acquire()`) initially. The number of available "permissions" can be
>>>> increased by calling `release()` after creating the semaphore. Call
>>>> `release()` 3 more times on our semaphore right away, and the initial 2
>>>> will increase to 5. So, 2 is not "max".
>>>>
>>>> The "max" is set by the template parameter `LeastMaxValue`.
>>>
>>> It seems I'm not the only one getting confused here. If I write:
>>>
>>> std::counting_semaphore<10> sem(2)
>>>
>>> then the semaphore can only be aquired TWO times, not 10. I don't understand
>>> what the 10 is supposed to do as changing the 10 to 0,1 or 1000 makes no
>>> difference whatsoever to how many times acquire() can be called before
>>> release() is called in the tests I've done.
>>
>> 10 is the maximum count the semaphore is allowed to reach, therefore
>> binary_semaphore, which is an alias-template to counting_semaphore<1>,
>> has a maximum count of one.
> 
> We're going around in circles. Instead of reading the man page how about
> trying some code. How many times to you think this loop will run before it
> hangs? 100 or 2?
> 
> #include <stdio.h>
> #include <semaphore>
> 
> std::counting_semaphore<100> sem(2);
> 
> int main()
> {
>          for(int i=0;;++i)
>          {
>                  sem.acquire();
>                  printf("Acquired %d\n",i);
>          }
>          return 0;
> }
> 
> Answer: 2
> 


Afaict, you set the initial state of the semaphore to two. Therefore 
acquire will block (actually deadlock here) after two calls as in your 
test code...

https://en.cppreference.com/w/cpp/thread/counting_semaphore
______________
As its name indicates, the LeastMaxValue is the minimum max value, not 
the actual max value. Thus max() can yield a number larger than 
LeastMaxValue.
______________

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