Warning: mysqli::__construct(): (HY000/1203): User howardkn already has more than 'max_user_connections' active connections in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\includes\artfuncs.php on line 21
Failed to connect to MySQL: (1203) User howardkn already has more than 'max_user_connections' active connections
Warning: mysqli::query(): Couldn't fetch mysqli in D:\Inetpub\vhosts\howardknight.net\al.howardknight.net\index.php on line 66
Article <vbsf2r$3miho$1@dont-email.me>
Deutsch   English   Français   Italiano  
<vbsf2r$3miho$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.lang.c++
Subject: Re: counting_semaphore question
Date: Wed, 11 Sep 2024 16:04:44 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 57
Message-ID: <vbsf2r$3miho$1@dont-email.me>
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>
Injection-Date: Wed, 11 Sep 2024 18:04:44 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="0c8b46a4b68c46aa75dd3a0e97bfee69";
	logging-data="3885624"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18YTkjBiP6QBKq1/CugvTJR"
Cancel-Lock: sha1:d5akTjOUZqisWYXn85u+sFTNMDA=
Bytes: 3044

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