Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Time is an illusion perpetrated by the manufacturers of space.


devel / comp.unix.programmer / Re: Whats the point of pthread_cond_broadcast() ?

SubjectAuthor
* Whats the point of pthread_cond_broadcast() ?Muttley
+* Re: Whats the point of pthread_cond_broadcast() ?Nicolas George
|`* Re: Whats the point of pthread_cond_broadcast() ?Muttley
| `* Re: Whats the point of pthread_cond_broadcast() ?Nicolas George
|  `- Re: Whats the point of pthread_cond_broadcast() ?Muttley
+* Re: Whats the point of pthread_cond_broadcast() ?Rainer Weikusat
|`* Re: Whats the point of pthread_cond_broadcast() ?Kaz Kylheku
| `- Re: Whats the point of pthread_cond_broadcast() ?Rainer Weikusat
+* Re: Whats the point of pthread_cond_broadcast() ?Kaz Kylheku
|`* Re: Whats the point of pthread_cond_broadcast() ?Muttley
| `* Re: Whats the point of pthread_cond_broadcast() ?Nicolas George
|  +* Re: Whats the point of pthread_cond_broadcast() ?Muttley
|  |`- Re: Whats the point of pthread_cond_broadcast() ?Nicolas George
|  `- Re: Whats the point of pthread_cond_broadcast() ?Kaz Kylheku
`* Re: Whats the point of pthread_cond_broadcast() ?Gary R. Schmidt
 `* Re: Whats the point of pthread_cond_broadcast() ?Muttley
  +* Re: Whats the point of pthread_cond_broadcast() ?Scott Lurndal
  |`* Re: Whats the point of pthread_cond_broadcast() ?Muttley
  | +* Re: Whats the point of pthread_cond_broadcast() ?Rainer Weikusat
  | |`- Re: Whats the point of pthread_cond_broadcast() ?Rainer Weikusat
  | `* Re: Whats the point of pthread_cond_broadcast() ?Kaz Kylheku
  |  +* Re: Whats the point of pthread_cond_broadcast() ?Rainer Weikusat
  |  |`* Re: Whats the point of pthread_cond_broadcast() ?Scott Lurndal
  |  | +- Re: Whats the point of pthread_cond_broadcast() ?Rainer Weikusat
  |  | `- Re: Whats the point of pthread_cond_broadcast() ?Kaz Kylheku
  |  `* Re: Whats the point of pthread_cond_broadcast() ?Muttley
  |   `* Re: Whats the point of pthread_cond_broadcast() ?Rainer Weikusat
  |    `* Re: Whats the point of pthread_cond_broadcast() ?Muttley
  |     +* Re: Whats the point of pthread_cond_broadcast() ?Scott Lurndal
  |     |`* Re: Whats the point of pthread_cond_broadcast() ?Muttley
  |     | +* Re: Whats the point of pthread_cond_broadcast() ?Scott Lurndal
  |     | |`* Re: Whats the point of pthread_cond_broadcast() ?Muttley
  |     | | +* Re: Whats the point of pthread_cond_broadcast() ?Scott Lurndal
  |     | | |+* Re: Whats the point of pthread_cond_broadcast() ?Muttley
  |     | | ||`* Re: Whats the point of pthread_cond_broadcast() ?Rainer Weikusat
  |     | | || `* Re: Whats the point of pthread_cond_broadcast() ?Muttley
  |     | | ||  +* Re: Whats the point of pthread_cond_broadcast() ?Scott Lurndal
  |     | | ||  |`* Re: Whats the point of pthread_cond_broadcast() ?Muttley
  |     | | ||  | `* Re: Whats the point of pthread_cond_broadcast() ?Rainer Weikusat
  |     | | ||  |  `* Re: Whats the point of pthread_cond_broadcast() ?Muttley
  |     | | ||  |   +* Re: Whats the point of pthread_cond_broadcast() ?Nicolas George
  |     | | ||  |   |`* Re: Whats the point of pthread_cond_broadcast() ?Muttley
  |     | | ||  |   | `* Re: Whats the point of pthread_cond_broadcast() ?Nicolas George
  |     | | ||  |   |  `- Re: Whats the point of pthread_cond_broadcast() ?Muttley
  |     | | ||  |   `- Re: Whats the point of pthread_cond_broadcast() ?Rainer Weikusat
  |     | | ||  `- Re: Whats the point of pthread_cond_broadcast() ?Rainer Weikusat
  |     | | |`* Re: Whats the point of pthread_cond_broadcast() ?Nicolas George
  |     | | | `* Re: Whats the point of pthread_cond_broadcast() ?Scott Lurndal
  |     | | |  `* Re: Whats the point of pthread_cond_broadcast() ?Muttley
  |     | | |   `- Re: Whats the point of pthread_cond_broadcast() ?Scott Lurndal
  |     | | `* Re: Whats the point of pthread_cond_broadcast() ?Kaz Kylheku
  |     | |  +* Re: Whats the point of pthread_cond_broadcast() ?Muttley
  |     | |  |`* Re: Whats the point of pthread_cond_broadcast() ?Kaz Kylheku
  |     | |  | `* Re: Whats the point of pthread_cond_broadcast() ?Muttley
  |     | |  |  +- Re: Whats the point of pthread_cond_broadcast() ?Rainer Weikusat
  |     | |  |  `- Re: Whats the point of pthread_cond_broadcast() ?Kaz Kylheku
  |     | |  `* Re: Whats the point of pthread_cond_broadcast() ?Scott Lurndal
  |     | |   `- Re: Whats the point of pthread_cond_broadcast() ?Muttley
  |     | +- Re: Whats the point of pthread_cond_broadcast() ?Rainer Weikusat
  |     | `- Re: Whats the point of pthread_cond_broadcast() ?Kaz Kylheku
  |     +- Re: Whats the point of pthread_cond_broadcast() ?Rainer Weikusat
  |     `* Re: Whats the point of pthread_cond_broadcast() ?Kaz Kylheku
  |      `* Re: Whats the point of pthread_cond_broadcast() ?Muttley
  |       +* Re: Whats the point of pthread_cond_broadcast() ?Gary R. Schmidt
  |       |`* Re: Whats the point of pthread_cond_broadcast() ?Muttley
  |       | `- Re: Whats the point of pthread_cond_broadcast() ?Scott Lurndal
  |       `* Re: Whats the point of pthread_cond_broadcast() ?Kaz Kylheku
  |        `* Re: Whats the point of pthread_cond_broadcast() ?Muttley
  |         +- Re: Whats the point of pthread_cond_broadcast() ?Rainer Weikusat
  |         `* Re: Whats the point of pthread_cond_broadcast() ?Kaz Kylheku
  |          `* Re: Whats the point of pthread_cond_broadcast() ?Muttley
  |           `* Re: Whats the point of pthread_cond_broadcast() ?Kaz Kylheku
  |            `* Re: Whats the point of pthread_cond_broadcast() ?Scott Lurndal
  |             `- Re: Whats the point of pthread_cond_broadcast() ?Muttley
  `* Re: Whats the point of pthread_cond_broadcast() ?Kaz Kylheku
   `- Re: Whats the point of pthread_cond_broadcast() ?Muttley

Pages:1234
Re: Whats the point of pthread_cond_broadcast() ?

<63e63335$0$24789$426a34cc@news.free.fr>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17500&group=comp.unix.programmer#17500

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!npeer.as286.net!npeer-ng0.as286.net!proxad.net!feeder1-1.proxad.net!cleanfeed1-a.proxad.net!nnrp5-1.free.fr!not-for-mail
Newsgroups: comp.unix.programmer
From: nicolas$george@salle-s.org (Nicolas George)
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Sender: george@phare.invalid (Nicolas George)
X-Newsreader: Flrn (0.9.20070704)
References: <trqgmc$312l3$1@dont-email.me> <ts3677$ng29$1@dont-email.me> <87cz6ibv04.fsf@doppelsaurus.mobileactivedefense.com> <ts55k0$10vn2$1@dont-email.me> <63e61c91$0$7649$426a74cc@news.free.fr> <ts58nd$11apa$1@dont-email.me>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=iso-8859-1
Date: 10 Feb 2023 12:06:14 GMT
Lines: 7
Message-ID: <63e63335$0$24789$426a34cc@news.free.fr>
Organization: Guest of ProXad - France
NNTP-Posting-Date: 10 Feb 2023 13:06:14 CET
NNTP-Posting-Host: 129.199.129.80
X-Trace: 1676030774 news-4.free.fr 24789 129.199.129.80:46342
X-Complaints-To: abuse@proxad.net
 by: Nicolas George - Fri, 10 Feb 2023 12:06 UTC

Muttley@dastardlyhq.com, dans le message <ts58nd$11apa$1@dont-email.me>,
a écrit :
> it describes how broadcast differs from signal when it didn't say any such
> thing.

It says such a thing, but you are obviously deep into Dunning-Kruger
territory when it comes to threads.

Re: Whats the point of pthread_cond_broadcast() ?

<87y1p58yum.fsf@doppelsaurus.mobileactivedefense.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17501&group=comp.unix.programmer#17501

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: rweikusat@talktalk.net (Rainer Weikusat)
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Fri, 10 Feb 2023 12:54:41 +0000
Lines: 41
Message-ID: <87y1p58yum.fsf@doppelsaurus.mobileactivedefense.com>
References: <trqgmc$312l3$1@dont-email.me> <trtviq$3qo84$1@dont-email.me>
<20230207110711.41@kylheku.com> <trvpl0$48dj$1@dont-email.me>
<87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com>
<ts0gdd$8207$1@dont-email.me> <_HPEL.638934$iU59.175216@fx14.iad>
<ts0h68$869b$1@dont-email.me> <IcQEL.127182$Ldj8.119120@fx47.iad>
<ts0jo2$8k6d$1@dont-email.me> <aHQEL.430433$gGD7.95617@fx11.iad>
<ts0l5t$8s8d$1@dont-email.me>
<877cws6p2m.fsf@doppelsaurus.mobileactivedefense.com>
<ts2ea5$kr94$1@dont-email.me> <vH7FL.176892$PXw7.109888@fx45.iad>
<ts3677$ng29$1@dont-email.me>
<87cz6ibv04.fsf@doppelsaurus.mobileactivedefense.com>
<ts55k0$10vn2$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net 9tjX2zNUBaDcQS1NhoHdig4G+qCvAKzIVGgc+UzkDwP6HA83I=
Cancel-Lock: sha1:imBogweVl7ny3wvHixRVqyhydME= sha1:4DAN+1q9TLDN4QsgQ8wQuLOWRlc=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Rainer Weikusat - Fri, 10 Feb 2023 12:54 UTC

Muttley@dastardlyhq.com writes:
> On Thu, 09 Feb 2023 17:37:15 +0000
> Rainer Weikusat <rweikusat@talktalk.net> wrote:
>>Muttley@dastardlyhq.com writes:
>>> scott@slp53.sl.home (Scott Lurndal) wrote:
>>>>Muttley@dastardlyhq.com writes:
>>
>>[...]
>>
>>>>>Clear would be:
>>>>>
>>>>>"Once the thread is woken up on the condition variable it then waits on
>>>>>the mutex AND DOES NOT EXIT pthread_cond_wait() until it aquires it."
>>>>>
>>>>>That is clarity, not the technogibberish above.
>>>>
>>>>The paragraph you call technogibberish has nothing to do with
>>>>what happens with the mutex, it's more of an internal implementation
>>>>detail and a warning to the implementer and the user of the effects of using
>>a
>>>>single
>>>>condition variable with different mutexes.
>>>>
>>>>This paragraph:
>>>>
>>>> Upon successful return, the mutex shall have been locked and
>>>> shall be owned by the calling thread.
>>>
>>> That also describe the behaviour with signal.
>>
>>Obviously, as the behaviour is identical in this regard.
>
> Exactly. So in trying to prove the man page was fine showed that even he
> hadn't read it properly.

Ehh ... no ... the implementation note is unrelated to this issue and
the difference between signal and broadcast is that the latter wakes up
all threads sleeping on the condition variable. As they must all acquire
the mutex before returning to the caller, it follows that they'll now
all be sleeping on the queue of that until each got its turn to hold
it.

Re: Whats the point of pthread_cond_broadcast() ?

<ts5p57$133r7$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17502&group=comp.unix.programmer#17502

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Fri, 10 Feb 2023 15:51:03 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <ts5p57$133r7$1@dont-email.me>
References: <trqgmc$312l3$1@dont-email.me> <ts3677$ng29$1@dont-email.me> <87cz6ibv04.fsf@doppelsaurus.mobileactivedefense.com> <ts55k0$10vn2$1@dont-email.me> <63e61c91$0$7649$426a74cc@news.free.fr> <ts58nd$11apa$1@dont-email.me> <63e63335$0$24789$426a34cc@news.free.fr>
Injection-Date: Fri, 10 Feb 2023 15:51:03 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="352b54c2c41889b1a4d39768c781fe19";
logging-data="1150823"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19DTXLq1S7grAHdily9G/5K"
Cancel-Lock: sha1:7oB5Ai4Hb56wPA1HJ56LmrtNz7o=
 by: Muttley@dastardlyhq.com - Fri, 10 Feb 2023 15:51 UTC

On 10 Feb 2023 12:06:14 GMT
Nicolas George <nicolas$george@salle-s.org> wrote:
>Muttley@dastardlyhq.com, dans le message <ts58nd$11apa$1@dont-email.me>,
> a �crit�:
>> it describes how broadcast differs from signal when it didn't say any such
>> thing.
>
>It says such a thing, but you are obviously deep into Dunning-Kruger
>territory when it comes to threads.

Whatever. I'm certainly tired of this thread.

Re: Whats the point of pthread_cond_broadcast() ?

<20230210221400.980@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17503&group=comp.unix.programmer#17503

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Sat, 11 Feb 2023 06:16:06 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <20230210221400.980@kylheku.com>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
<trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad>
<trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com>
<87v8kdp5c5.fsf@doppelsaurus.mobileactivedefense.com>
<45zEL.446898$Tcw8.261380@fx10.iad>
Injection-Date: Sat, 11 Feb 2023 06:16:06 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="d243eb6064a9d409a96b4c737365830d";
logging-data="1425120"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX191PlL2ZKNHJiqzb/w8BUd1dwIXHCJEfaU="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:HtqdKG90oZ43lzedqc1OV6nyZuI=
 by: Kaz Kylheku - Sat, 11 Feb 2023 06:16 UTC

On 2023-02-07, Scott Lurndal <scott@slp53.sl.home> wrote:
> Rainer Weikusat <rweikusat@talktalk.net> writes:
>>Kaz Kylheku <864-117-4973@kylheku.com> writes:
>>> On 2023-02-07, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>>>> Its also invisible to the application program so I still don't see
>>>> what the difference and/or advantage of using broadcast vs signal is
>>>> to the application.
>>>
>>> It is completely visible to the application. Compare:
>>>
>>> "Ten threads have been woken up and are contending for the mutex in
>>> order the all return from their respective pthread_cond_wait calls."
>>>
>>> "One threads has been woken up and is contending for the mutex in
>>> order to return from pthread_cond_wait."
>>
>>The thread is not contending for the mutex. Assuming the wakeup call was
>>done with the mutex unlocked, it'll just acquire it without contention
>>and without again having to enter the kernel and can just continue
>>running.
>
> The thread (and all other threads released by 'broadcast') will be
> contending for the mutex. And it can't "acquire it without contention"
> because they may be other threads, not currently waiting on the condition
> variable, one of which may be holding the mutex.

Well, it can; if the time spent in the mutex after the pthread_cond_wait
is very short, it could be that it's entirely uncontended.

On a single processor it could happen in an obvious way: only one thread
at a time runs. Several threads are made runnable by the broadcast.
One is at th ehead of the run queue. It acquires the mutex, returns from
pthread_cond_wait, does something in the mutex and then calls
pthread_mutex_unlock. Its time quantum is nowhere near up yet; the other
threads are still in the run queue. As they are dispatched by the
scheduler one by one, it's entirely possible they each find the mutex
similarly uncontended.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Whats the point of pthread_cond_broadcast() ?

<20230210222026.910@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17504&group=comp.unix.programmer#17504

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Sat, 11 Feb 2023 06:30:46 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <20230210222026.910@kylheku.com>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
<trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad>
<trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com>
<trvpl0$48dj$1@dont-email.me>
<87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com>
<ts0gdd$8207$1@dont-email.me>
Injection-Date: Sat, 11 Feb 2023 06:30:46 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="d243eb6064a9d409a96b4c737365830d";
logging-data="1425120"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/dYRo1FdKUSFOtlCi/f4NoVc9/F22DATQ="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:Dt5wqIV35FE8M4T1kCAGYbeqkRo=
 by: Kaz Kylheku - Sat, 11 Feb 2023 06:30 UTC

On 2023-02-08, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
> On Wed, 08 Feb 2023 14:56:47 +0000
> Rainer Weikusat <rweikusat@talktalk.net> wrote:
>>Muttley@dastardlyhq.com writes:
>>> I don't have time to write boilerplate to get this up and running but I
>>> can't see how using broadcast instead of signal makes any difference
>>> in how the code operates.
>>
>>As written, the code is correct with _broadcast and wouldn't be with
>>signal. Any number of threads can be waiting for the api to become
>>ready. Hence, whatever threads are actually waiting by that time must be
>>woken up as they'll otherwise sleep forever.
>
> Yes, they all get woken up then they all go back to sleep again except 1
> without doing anything. Sorry, still not seeing it.

No, threads woken up by pthread_cond_broadcast do not go back to sleep;
they all return from the pthread_cond_wait function. That function call has
a loop around it; so they will go back to sleep only if the loop
iterates again. The loop is guarded by the while (!api_ready) condition
which was set true before the pthread_cond_broadcast call, So all the
threads returning from the condition wait will see the true value and
bail out of the loop, returning from api_ready_wait.

The loop is is in the critical region protected by the mutex, so that
only one thread at a time can be checking while (!api_ready).
That's a separate consideration. "All threads return from
pthread_cond_wait" doesn't mean they do that concurrently; since a
thread returning from that function owns the mutex, all threads
returning from a condition wait against the same mutex are serialized.
Being serialized is not the same thing as being indefinitely blocked.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Whats the point of pthread_cond_broadcast() ?

<20230210223411.776@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17505&group=comp.unix.programmer#17505

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Sat, 11 Feb 2023 06:45:08 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <20230210223411.776@kylheku.com>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
<trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad>
<trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com>
<trvpl0$48dj$1@dont-email.me>
<87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com>
<ts0gdd$8207$1@dont-email.me> <_HPEL.638934$iU59.175216@fx14.iad>
<ts0h68$869b$1@dont-email.me>
Injection-Date: Sat, 11 Feb 2023 06:45:08 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="d243eb6064a9d409a96b4c737365830d";
logging-data="1425120"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/URPvLEXC0W0O2J1oBXUoo0STZPsfr8fg="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:JLA5X3pbKaG/ihhoapDPV00ar20=
 by: Kaz Kylheku - Sat, 11 Feb 2023 06:45 UTC

On 2023-02-08, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
> If they're all sleeping on a mutex you don't need condition variables to
> start with.

1. condition varaibles exist because you can't do everything with mutexes.

2. the idea they are sleeping on the mutex is a simplification; it's
possible that the threads woken by the broadcast can each get the mutex
without waiting for it. This will happen on a single processor, if the
critical region is short:

E.g. here it is very short, just a few instructions:

while (!api_ready)
pthread_cond_wait(...)

pthread_mutex_unlock(...)

the returning thread acquires the mutex, examines api_ready,
finds it to be true, and immediately gives up the mutex.

You have to use multiple processors to get contention. On a single
processor, pthread_cond_broadcast will transfer all the waiting threads
to the run queue. The scheduler will then serialize their execution,
because only one thread can have the CPU at a time. So the first thread
gets the CPU, which may give it a time quantum of up to several
milliseconds or whatever. During that time quantum, it gets the mutex,
checks the flag and releases it. None of the other woken up threads
have yet executed, yet the first one is already done with the mutex;
it's returned out of api_ready_wait and has moved on to other work.
When the second thread is dispatched, it may well find the mutex
unlocked, and ditto with the third and subsequent.

> Whether you use signal or broadcast ONLY ONE thread exits the
> wait. The behaviour from an application program POV is identical.

If people are escaping from a burning a building, don't you think there
is a difference between "one person at a time jumps out of the window
onto the life net so they are all saved" versus "only one person manages
to jump out of the window to safety?"

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Whats the point of pthread_cond_broadcast() ?

<20230210224534.43@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17506&group=comp.unix.programmer#17506

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Sat, 11 Feb 2023 07:13:26 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <20230210224534.43@kylheku.com>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
<trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad>
<trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com>
<trvpl0$48dj$1@dont-email.me>
<87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com>
<ts0gdd$8207$1@dont-email.me> <_HPEL.638934$iU59.175216@fx14.iad>
<ts0h68$869b$1@dont-email.me> <IcQEL.127182$Ldj8.119120@fx47.iad>
<ts0jo2$8k6d$1@dont-email.me>
Injection-Date: Sat, 11 Feb 2023 07:13:26 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="d243eb6064a9d409a96b4c737365830d";
logging-data="1435371"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ehABic4Kal6QDuNOCM+ML5EtP1lyzxMk="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:ZZqOiInx5xFVko4H/qezoUlvCwY=
 by: Kaz Kylheku - Sat, 11 Feb 2023 07:13 UTC

On 2023-02-08, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
> Nowhere have I read that inside pthread_cond_wait() there are 2
> possible thread states which you have to be aware of.

No, there aren't. Condition variablews are specifically stateless (or
appear that way to the application), which make them easy to use. You
don't have to think whether a condition variable is in some signalled
versus non-signalled state.

> What a bloody awful API design.

What is your proposal to fix it, or make an alternative?

Condition variables are directly derived from an invention called
"monitors and condition variables", by C. A. R. Hoare.
(Monitors and condition variables have some different properties
from POSIX-like mutexes and condition variables; there are certain
guarantees so that while loops are not required around condition waits.)

Anyway the API design comes more or less from the theory, which dictates
what it looks like.

Microsoft eventually added condition variables to Windows, and look,
the API is the same, under different names.

https://learn.microsoft.com/en-us/windows/win32/sync/condition-variables

If you think conditions are mutexes are complicated to use, you can use
something else. Conditions are very good for implementing other
kinds of primitives and being sure you got it right.

E.g. it's pretty trivial to make a correct counting semaphore with
a mutex and condition. The reverse isn't true; implementing conditions
with semaphores is a tricky exercise.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Whats the point of pthread_cond_broadcast() ?

<ts7ob7$1cmdu$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17507&group=comp.unix.programmer#17507

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Sat, 11 Feb 2023 09:49:28 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <ts7ob7$1cmdu$1@dont-email.me>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
<trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad>
<trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com>
<trvpl0$48dj$1@dont-email.me>
<87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com>
<ts0gdd$8207$1@dont-email.me>
<20230210222026.910@kylheku.com>
Injection-Date: Sat, 11 Feb 2023 09:49:28 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="c6259bcea17f18270d0daa03cadc3c36";
logging-data="1464766"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18EoDfkAckjmP4i+xv4haIt"
Cancel-Lock: sha1:PwTXCUG0D0HxURGNprYKUn/35Dg=
 by: Muttley@dastardlyhq.com - Sat, 11 Feb 2023 09:49 UTC

On Sat, 11 Feb 2023 06:30:46 -0000 (UTC)
Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>On 2023-02-08, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>> On Wed, 08 Feb 2023 14:56:47 +0000
>> Rainer Weikusat <rweikusat@talktalk.net> wrote:
>>>Muttley@dastardlyhq.com writes:
>>>> I don't have time to write boilerplate to get this up and running but I
>>>> can't see how using broadcast instead of signal makes any difference
>>>> in how the code operates.
>>>
>>>As written, the code is correct with _broadcast and wouldn't be with
>>>signal. Any number of threads can be waiting for the api to become
>>>ready. Hence, whatever threads are actually waiting by that time must be
>>>woken up as they'll otherwise sleep forever.
>>
>> Yes, they all get woken up then they all go back to sleep again except 1
>> without doing anything. Sorry, still not seeing it.
>
>No, threads woken up by pthread_cond_broadcast do not go back to sleep;
>they all return from the pthread_cond_wait function. That function call has

No they don't all return, only 1 does.

Its interesting that hardly anyone really understands the difference between
the signal and broadcast and to me that is indicative of a very poorly designed
API.

Re: Whats the point of pthread_cond_broadcast() ?

<ts7oku$1cncg$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17508&group=comp.unix.programmer#17508

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Sat, 11 Feb 2023 09:54:38 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <ts7oku$1cncg$1@dont-email.me>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
<trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad>
<trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com>
<trvpl0$48dj$1@dont-email.me>
<87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com>
<ts0gdd$8207$1@dont-email.me> <_HPEL.638934$iU59.175216@fx14.iad>
<ts0h68$869b$1@dont-email.me> <IcQEL.127182$Ldj8.119120@fx47.iad>
<ts0jo2$8k6d$1@dont-email.me>
<20230210224534.43@kylheku.com>
Injection-Date: Sat, 11 Feb 2023 09:54:38 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="c6259bcea17f18270d0daa03cadc3c36";
logging-data="1465744"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/oQQpgTKVjdriDK3/tpc/I"
Cancel-Lock: sha1:xRvj3TM+u2jsEOz2UDsytdX+L0U=
 by: Muttley@dastardlyhq.com - Sat, 11 Feb 2023 09:54 UTC

On Sat, 11 Feb 2023 07:13:26 -0000 (UTC)
Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>On 2023-02-08, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>> Nowhere have I read that inside pthread_cond_wait() there are 2
>> possible thread states which you have to be aware of.
>
>No, there aren't. Condition variablews are specifically stateless (or
>appear that way to the application), which make them easy to use. You

I meant the *function* has 2 states:
1) Waiting on the condition variable
2) Waiting on the mutex

It would seem that for signal only 1 thread goes from 1 -> 2 whereas with
broadcast ALL threads go from 1 -> 2 but only ONE thread exits the wait()
function in either case.

>don't have to think whether a condition variable is in some signalled
>versus non-signalled state.
>
>> What a bloody awful API design.
>
>What is your proposal to fix it, or make an alternative?

Apperently there is an alternative in pthreads someone mentioned called
barriers but its not mandatory and MacOS doesn't support them. However
they don't seem to have this 2 state locking nonsense.

>Condition variables are directly derived from an invention called
>"monitors and condition variables", by C. A. R. Hoare.
>(Monitors and condition variables have some different properties
>from POSIX-like mutexes and condition variables; there are certain
>guarantees so that while loops are not required around condition waits.)
>
>Anyway the API design comes more or less from the theory, which dictates
>what it looks like.

Thats the trouble with theories - they don't always translate into real world
use very well.

>E.g. it's pretty trivial to make a correct counting semaphore with
>a mutex and condition. The reverse isn't true; implementing conditions
>with semaphores is a tricky exercise.

SysV semaphores do a good job but the API is a mess.

Re: Whats the point of pthread_cond_broadcast() ?

<8oakbj-5o2.ln1@paranoia.mcleod-schmidt.id.au>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17509&group=comp.unix.programmer#17509

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: grschmidt@acm.org (Gary R. Schmidt)
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Sat, 11 Feb 2023 22:28:48 +1100
Lines: 38
Message-ID: <8oakbj-5o2.ln1@paranoia.mcleod-schmidt.id.au>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au> <trts7n$3q3jn$1@dont-email.me>
<y3vEL.624380$iU59.162684@fx14.iad> <trtviq$3qo84$1@dont-email.me>
<20230207110711.41@kylheku.com> <trvpl0$48dj$1@dont-email.me>
<87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com>
<ts0gdd$8207$1@dont-email.me> <20230210222026.910@kylheku.com>
<ts7ob7$1cmdu$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net yPAHI8Mi/TwDFHmVBloYLAqMHLTnkAE8DbYbHgVKqDvKlQiv8=
X-Orig-Path: paranoia.mcleod-schmidt.id.au!not-for-mail
Cancel-Lock: sha1:AqMm+YRuXiEBYFV1VUd5yekgDlo=
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Betterbird/102.7.0
Content-Language: en-AU
In-Reply-To: <ts7ob7$1cmdu$1@dont-email.me>
X-Clacks-Overhead: GNU Terry Pratchett
 by: Gary R. Schmidt - Sat, 11 Feb 2023 11:28 UTC

On 11/02/2023 20:49, Muttley@dastardlyhq.com wrote:
> On Sat, 11 Feb 2023 06:30:46 -0000 (UTC)
> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>> On 2023-02-08, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>>> On Wed, 08 Feb 2023 14:56:47 +0000
>>> Rainer Weikusat <rweikusat@talktalk.net> wrote:
>>>> Muttley@dastardlyhq.com writes:
>>>>> I don't have time to write boilerplate to get this up and running but I
>>>>> can't see how using broadcast instead of signal makes any difference
>>>>> in how the code operates.
>>>>
>>>> As written, the code is correct with _broadcast and wouldn't be with
>>>> signal. Any number of threads can be waiting for the api to become
>>>> ready. Hence, whatever threads are actually waiting by that time must be
>>>> woken up as they'll otherwise sleep forever.
>>>
>>> Yes, they all get woken up then they all go back to sleep again except 1
>>> without doing anything. Sorry, still not seeing it.
>>
>> No, threads woken up by pthread_cond_broadcast do not go back to sleep;
>> they all return from the pthread_cond_wait function. That function call has
>
> No they don't all return, only 1 does.
>
That does not tally with my experience.

What Operating System, programming language, and pthreads library are
you using? (You may have mentioned these things ages ago, I don't recall.)

> Its interesting that hardly anyone really understands the difference between
> the signal and broadcast and to me that is indicative of a very poorly designed
> API.
>
Well, those of us who have been using pthreads for a few decades seem to
have no trouble.

Cheers,
Gary B-)

Re: Whats the point of pthread_cond_broadcast() ?

<ts7ude$1d9bu$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17510&group=comp.unix.programmer#17510

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Sat, 11 Feb 2023 11:33:03 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <ts7ude$1d9bu$1@dont-email.me>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au> <trts7n$3q3jn$1@dont-email.me>
<y3vEL.624380$iU59.162684@fx14.iad> <trtviq$3qo84$1@dont-email.me>
<20230207110711.41@kylheku.com> <trvpl0$48dj$1@dont-email.me>
<87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com>
<ts0gdd$8207$1@dont-email.me> <20230210222026.910@kylheku.com>
<ts7ob7$1cmdu$1@dont-email.me>
<8oakbj-5o2.ln1@paranoia.mcleod-schmidt.id.au>
Injection-Date: Sat, 11 Feb 2023 11:33:03 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="c6259bcea17f18270d0daa03cadc3c36";
logging-data="1484158"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+DCCYiDHfAKU5kN7+soMQ9"
Cancel-Lock: sha1:BUgUkfUtHD5r13pvPyjP9a8kIH4=
 by: Muttley@dastardlyhq.com - Sat, 11 Feb 2023 11:33 UTC

On Sat, 11 Feb 2023 22:28:48 +1100
"Gary R. Schmidt" <grschmidt@acm.org> wrote:
>On 11/02/2023 20:49, Muttley@dastardlyhq.com wrote:
>> On Sat, 11 Feb 2023 06:30:46 -0000 (UTC)
>> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>> On 2023-02-08, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>>>> On Wed, 08 Feb 2023 14:56:47 +0000
>>>> Rainer Weikusat <rweikusat@talktalk.net> wrote:
>>>>> Muttley@dastardlyhq.com writes:
>>>>>> I don't have time to write boilerplate to get this up and running but I
>>>>>> can't see how using broadcast instead of signal makes any difference
>>>>>> in how the code operates.
>>>>>
>>>>> As written, the code is correct with _broadcast and wouldn't be with
>>>>> signal. Any number of threads can be waiting for the api to become
>>>>> ready. Hence, whatever threads are actually waiting by that time must be
>>>>> woken up as they'll otherwise sleep forever.
>>>>
>>>> Yes, they all get woken up then they all go back to sleep again except 1
>>>> without doing anything. Sorry, still not seeing it.
>>>
>>> No, threads woken up by pthread_cond_broadcast do not go back to sleep;
>>> they all return from the pthread_cond_wait function. That function call has
>>
>> No they don't all return, only 1 does.
>>
>That does not tally with my experience.

I don't believe you. If all threads exited the wait at the same time that
would mean they'd all acquired a lock on the same mutex at the same time.

>What Operating System, programming language, and pthreads library are
>you using? (You may have mentioned these things ages ago, I don't recall.)

MacOS.
>Well, those of us who have been using pthreads for a few decades seem to
>have no trouble.

I'd revisit the pthread_cond_wait man page if I were you.

Re: Whats the point of pthread_cond_broadcast() ?

<MCPFL.525256$Tcw8.83497@fx10.iad>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17511&group=comp.unix.programmer#17511

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx10.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Newsgroups: comp.unix.programmer
References: <trqgmc$312l3$1@dont-email.me> <5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au> <trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad> <trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com> <trvpl0$48dj$1@dont-email.me> <87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com> <ts0gdd$8207$1@dont-email.me> <20230210222026.910@kylheku.com> <ts7ob7$1cmdu$1@dont-email.me> <8oakbj-5o2.ln1@paranoia.mcleod-schmidt.id.au> <ts7ude$1d9bu$1@dont-email.me>
Lines: 39
Message-ID: <MCPFL.525256$Tcw8.83497@fx10.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Sat, 11 Feb 2023 16:45:32 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Sat, 11 Feb 2023 16:45:32 GMT
X-Received-Bytes: 3022
 by: Scott Lurndal - Sat, 11 Feb 2023 16:45 UTC

Muttley@dastardlyhq.com writes:
>On Sat, 11 Feb 2023 22:28:48 +1100
>"Gary R. Schmidt" <grschmidt@acm.org> wrote:
>>On 11/02/2023 20:49, Muttley@dastardlyhq.com wrote:
>>> On Sat, 11 Feb 2023 06:30:46 -0000 (UTC)
>>> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>>> On 2023-02-08, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>>>>> On Wed, 08 Feb 2023 14:56:47 +0000
>>>>> Rainer Weikusat <rweikusat@talktalk.net> wrote:
>>>>>> Muttley@dastardlyhq.com writes:
>>>>>>> I don't have time to write boilerplate to get this up and running but I
>>>>>>> can't see how using broadcast instead of signal makes any difference
>>>>>>> in how the code operates.
>>>>>>
>>>>>> As written, the code is correct with _broadcast and wouldn't be with
>>>>>> signal. Any number of threads can be waiting for the api to become
>>>>>> ready. Hence, whatever threads are actually waiting by that time must be
>>>>>> woken up as they'll otherwise sleep forever.
>>>>>
>>>>> Yes, they all get woken up then they all go back to sleep again except 1
>>>>> without doing anything. Sorry, still not seeing it.
>>>>
>>>> No, threads woken up by pthread_cond_broadcast do not go back to sleep;
>>>> they all return from the pthread_cond_wait function. That function call has
>>>
>>> No they don't all return, only 1 does.
>>>
>>That does not tally with my experience.
>
>I don't believe you. If all threads exited the wait at the same time that
>would mean they'd all acquired a lock on the same mutex at the same time.

Gary wrote that they exited the wait. By that, he was referring to
the internal implementation of pthread_cond_wait, he did not intend
to imply that the they returned from the pthread_cond_wait call;
clearly only one of the threads racing on the mutex will acquire
it, thus only one thread can return from pthread_cond_wait and
the remainder will wait for that thread to release the mutex
either explicitly or implicitly (e.g. calling pthread_cond_wait).

Re: Whats the point of pthread_cond_broadcast() ?

<ORPFL.151825$Olad.20373@fx35.iad>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17512&group=comp.unix.programmer#17512

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx35.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Newsgroups: comp.unix.programmer
References: <trqgmc$312l3$1@dont-email.me> <5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au> <trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad> <trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com> <trvpl0$48dj$1@dont-email.me> <87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com> <ts0gdd$8207$1@dont-email.me> <_HPEL.638934$iU59.175216@fx14.iad> <ts0h68$869b$1@dont-email.me> <IcQEL.127182$Ldj8.119120@fx47.iad> <ts0jo2$8k6d$1@dont-email.me> <20230210224534.43@kylheku.com>
Lines: 49
Message-ID: <ORPFL.151825$Olad.20373@fx35.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Sat, 11 Feb 2023 17:01:34 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Sat, 11 Feb 2023 17:01:34 GMT
X-Received-Bytes: 3461
 by: Scott Lurndal - Sat, 11 Feb 2023 17:01 UTC

Kaz Kylheku <864-117-4973@kylheku.com> writes:
>On 2023-02-08, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>> Nowhere have I read that inside pthread_cond_wait() there are 2
>> possible thread states which you have to be aware of.
>
>No, there aren't. Condition variablews are specifically stateless (or
>appear that way to the application), which make them easy to use. You
>don't have to think whether a condition variable is in some signalled
>versus non-signalled state.
>
>> What a bloody awful API design.
>
>What is your proposal to fix it, or make an alternative?
>
>Condition variables are directly derived from an invention called
>"monitors and condition variables", by C. A. R. Hoare.
>(Monitors and condition variables have some different properties
>from POSIX-like mutexes and condition variables; there are certain
>guarantees so that while loops are not required around condition waits.)

There was an early unix implementation of mutexes and condition
variables in Ultrix in the mid-80's when it added support for
multithreaded applications. Members of that team participated
heavily in the posix 1003.4 development that resulted in pthreads.

VMS on the other hand, used a more heavy-weight, but rather flexible
waiting mechanism called event flags, which Cutler brought over
when designing NT. The event flags interface allowed one to wait
on a set of events, rather than the single event that pthread condition
variables support.

Earlier mainframes had custom wait-for-event interfaces, such as
Burroughs complex wait (a more generalized version of the unix
poll(2) or select(2) system calls which allowed the application
to wait for a wide variety of events atomically, such as
I/O complete for a card reader, a timer, a message from another
application, input from a datacomm terminal, host-to-host
communications or a network packet.

Along with Hoare, was CSP (Cooperating Sequential Processes)
by Dykstra wherein he coined the term 'loosely connected processes',
or what we'd call today multithreaded processes and introduced
the concept of the semaphore as a synchronization primitive.

Then there were the distributed synchronization algorithms
espoused by Leslie Lamport (particularly with respect to
obtaining a consistent and rational view of time).

https://www.cs.utexas.edu/users/EWD/transcriptions/EWD01xx/EWD123.html

Re: Whats the point of pthread_cond_broadcast() ?

<ts8i8s$1fc2j$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17513&group=comp.unix.programmer#17513

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Sat, 11 Feb 2023 17:11:56 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <ts8i8s$1fc2j$1@dont-email.me>
References: <trqgmc$312l3$1@dont-email.me> <5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au> <trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad> <trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com> <trvpl0$48dj$1@dont-email.me> <87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com> <ts0gdd$8207$1@dont-email.me> <_HPEL.638934$iU59.175216@fx14.iad> <ts0h68$869b$1@dont-email.me> <IcQEL.127182$Ldj8.119120@fx47.iad> <ts0jo2$8k6d$1@dont-email.me> <20230210224534.43@kylheku.com>
<ORPFL.151825$Olad.20373@fx35.iad>
Injection-Date: Sat, 11 Feb 2023 17:11:56 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="c6259bcea17f18270d0daa03cadc3c36";
logging-data="1552467"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/LhGHW5sJgtggw7K2XB6dU"
Cancel-Lock: sha1:X1G5A98SlIKM/lCJUUo2B6VV5LM=
 by: Muttley@dastardlyhq.com - Sat, 11 Feb 2023 17:11 UTC

On Sat, 11 Feb 2023 17:01:34 GMT
scott@slp53.sl.home (Scott Lurndal) wrote:
>Kaz Kylheku <864-117-4973@kylheku.com> writes:
>>Condition variables are directly derived from an invention called
>>"monitors and condition variables", by C. A. R. Hoare.
>>(Monitors and condition variables have some different properties
>>from POSIX-like mutexes and condition variables; there are certain
>>guarantees so that while loops are not required around condition waits.)
>
>There was an early unix implementation of mutexes and condition
>variables in Ultrix in the mid-80's when it added support for
>multithreaded applications. Members of that team participated
>heavily in the posix 1003.4 development that resulted in pthreads.
>
>VMS on the other hand, used a more heavy-weight, but rather flexible
>waiting mechanism called event flags, which Cutler brought over
>when designing NT. The event flags interface allowed one to wait

Shame he didn't port VMS's reliability too.

Re: Whats the point of pthread_cond_broadcast() ?

<20230211213210.234@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17514&group=comp.unix.programmer#17514

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Sun, 12 Feb 2023 05:35:48 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <20230211213210.234@kylheku.com>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
<trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad>
<trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com>
<trvpl0$48dj$1@dont-email.me>
<87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com>
<ts0gdd$8207$1@dont-email.me> <20230210222026.910@kylheku.com>
<ts7ob7$1cmdu$1@dont-email.me>
Injection-Date: Sun, 12 Feb 2023 05:35:48 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="a270de79fe4e2d3e4b4cbcc4997a2b87";
logging-data="1792295"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/uIG1bOhCQO0TQq9bJkniaZkXlEFsKjQM="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:CV88li7RPalXDG6m8bk/G534+5E=
 by: Kaz Kylheku - Sun, 12 Feb 2023 05:35 UTC

On 2023-02-11, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
> On Sat, 11 Feb 2023 06:30:46 -0000 (UTC)
> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>No, threads woken up by pthread_cond_broadcast do not go back to sleep;
>>they all return from the pthread_cond_wait function. That function call has
>
> No they don't all return, only 1 does.

They all return, just not at the same time. Obviously, if the first
thread which returns does something unusual, like never unlocking the
mutex, then the others are stuck. That's almost certainly going to only
happen due to a bug.

I have the impression you are just trying to rearrange the verbal
expressions of concepts instead of thinking in terms of the actual
concurrency.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Whats the point of pthread_cond_broadcast() ?

<20230211213600.637@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17515&group=comp.unix.programmer#17515

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Sun, 12 Feb 2023 06:27:07 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 79
Message-ID: <20230211213600.637@kylheku.com>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
<trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad>
<trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com>
<trvpl0$48dj$1@dont-email.me>
<87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com>
<ts0gdd$8207$1@dont-email.me> <_HPEL.638934$iU59.175216@fx14.iad>
<ts0h68$869b$1@dont-email.me> <IcQEL.127182$Ldj8.119120@fx47.iad>
<ts0jo2$8k6d$1@dont-email.me> <20230210224534.43@kylheku.com>
<ts7oku$1cncg$1@dont-email.me>
Injection-Date: Sun, 12 Feb 2023 06:27:07 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="a270de79fe4e2d3e4b4cbcc4997a2b87";
logging-data="1802800"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+bSgSq6tTCmlDMNTxYJNcbuvHeNgBaVS0="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:48eo1ogcEiWg7/Sw3UWHz/SNtpc=
 by: Kaz Kylheku - Sun, 12 Feb 2023 06:27 UTC

On 2023-02-11, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
> On Sat, 11 Feb 2023 07:13:26 -0000 (UTC)
> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>On 2023-02-08, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>>> Nowhere have I read that inside pthread_cond_wait() there are 2
>>> possible thread states which you have to be aware of.
>>
>>No, there aren't. Condition variablews are specifically stateless (or
>>appear that way to the application), which make them easy to use. You
>
> I meant the *function* has 2 states:
> 1) Waiting on the condition variable
> 2) Waiting on the mutex

Such states are not externally observable. There is literally no
standard API by which you can tell, and application logic isn't
required to handle any such states.

> It would seem that for signal only 1 thread goes from 1 -> 2 whereas with
> broadcast ALL threads go from 1 -> 2 but only ONE thread exits the wait()
> function in either case.

That is not the proper understanding. Whether (2) waiting on the mutex
happens is a matter of chance: it depends on factors like how many
processors are there, and how long is the mutex being held.

All those threads will go through the mutex, taking turns owning it; but
that doesn't imply they were ever waiting on it. Mutex locks can be
acquired without waiting.

>>don't have to think whether a condition variable is in some signalled
>>versus non-signalled state.
>>
>>> What a bloody awful API design.
>>
>>What is your proposal to fix it, or make an alternative?
>
> Apperently there is an alternative in pthreads someone mentioned called

That was me.

> barriers but its not mandatory and MacOS doesn't support them.

That's pretty lame; I wrote the first glibc/linux version of
pthread_barrier_wait in 2001, now 22 years ago. Kids born then
are graduating with degrees.

Be that as it may, you can quite easily make barriers if you have
mutexes/conditions. A program depending on pthread_barrier_wait can
easily supply its own implementation to be ported to a system where that
is missing.

Barriers are not an alternative to conditions.

> However they don't seem to have this 2 state locking nonsense.

Barriers are also not useful in many situations in which conditions
are. They have their own limitations. You have to declare how many
threads are using a barrier. When that many threads wait on it, the
barrier triggers, letting them all go. One of the threads is selected
(perhaps randomly) as the "serial thread": it receives a different
return value informing it.

This is useful for parallel processing scenarios, where you have all the
threads doing some portion of the same overall task, like some sort
of parallelized number crunching.

>>E.g. it's pretty trivial to make a correct counting semaphore with
>>a mutex and condition. The reverse isn't true; implementing conditions
>>with semaphores is a tricky exercise.
>
> SysV semaphores do a good job but the API is a mess.

Good job of what? Implementing condition variables?

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Whats the point of pthread_cond_broadcast() ?

<tsacvv$1o5ka$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17516&group=comp.unix.programmer#17516

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Sun, 12 Feb 2023 09:54:07 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <tsacvv$1o5ka$1@dont-email.me>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
<trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad>
<trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com>
<trvpl0$48dj$1@dont-email.me>
<87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com>
<ts0gdd$8207$1@dont-email.me> <20230210222026.910@kylheku.com>
<ts7ob7$1cmdu$1@dont-email.me>
<20230211213210.234@kylheku.com>
Injection-Date: Sun, 12 Feb 2023 09:54:07 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="bcbd279f19dfaa5af38cf603fbbabf12";
logging-data="1840778"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19I1PFNcDMWJE9PrP8fwQBN"
Cancel-Lock: sha1:WtYsTL6p0m6CI9sRwmXWlUban0E=
 by: Muttley@dastardlyhq.com - Sun, 12 Feb 2023 09:54 UTC

On Sun, 12 Feb 2023 05:35:48 -0000 (UTC)
Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>On 2023-02-11, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>> On Sat, 11 Feb 2023 06:30:46 -0000 (UTC)
>> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>>No, threads woken up by pthread_cond_broadcast do not go back to sleep;
>>>they all return from the pthread_cond_wait function. That function call has
>>
>> No they don't all return, only 1 does.
>
>They all return, just not at the same time. Obviously, if the first
>thread which returns does something unusual, like never unlocking the
>mutex, then the others are stuck. That's almost certainly going to only
>happen due to a bug.

Why is it a bug? It may be a long time before the first exiting the wait
unlocks the mutex depending on what the program does. The fact remains that
only one thread exits the wait at a time regardless of whether you send a
signal or broadcast. The disctinction in the behaviour is confusing and IMO
its a poorly designed API. For a start if all you want to do is control
threads stopping and starting without potentially having a deadlock with the
controlling thread there's no need to use mutexes in the first place.

Re: Whats the point of pthread_cond_broadcast() ?

<tsad8u$1o6e6$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17517&group=comp.unix.programmer#17517

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Sun, 12 Feb 2023 09:58:54 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 57
Message-ID: <tsad8u$1o6e6$1@dont-email.me>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
<trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad>
<trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com>
<trvpl0$48dj$1@dont-email.me>
<87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com>
<ts0gdd$8207$1@dont-email.me> <_HPEL.638934$iU59.175216@fx14.iad>
<ts0h68$869b$1@dont-email.me> <IcQEL.127182$Ldj8.119120@fx47.iad>
<ts0jo2$8k6d$1@dont-email.me> <20230210224534.43@kylheku.com>
<ts7oku$1cncg$1@dont-email.me>
<20230211213600.637@kylheku.com>
Injection-Date: Sun, 12 Feb 2023 09:58:54 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="bcbd279f19dfaa5af38cf603fbbabf12";
logging-data="1841606"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX193D90PiY6rEHV0XUEugGfy"
Cancel-Lock: sha1:DCb+I+b9EzV8TT/yHYhAI50T5p0=
 by: Muttley@dastardlyhq.com - Sun, 12 Feb 2023 09:58 UTC

On Sun, 12 Feb 2023 06:27:07 -0000 (UTC)
Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>On 2023-02-11, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>> On Sat, 11 Feb 2023 07:13:26 -0000 (UTC)
>> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>>On 2023-02-08, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>>>> Nowhere have I read that inside pthread_cond_wait() there are 2
>>>> possible thread states which you have to be aware of.
>>>
>>>No, there aren't. Condition variablews are specifically stateless (or
>>>appear that way to the application), which make them easy to use. You
>>
>> I meant the *function* has 2 states:
>> 1) Waiting on the condition variable
>> 2) Waiting on the mutex
>
>Such states are not externally observable. There is literally no
>standard API by which you can tell, and application logic isn't
>required to handle any such states.

No, but the application logic has to be different depending on whether you're
using signal or broadcast.

>> It would seem that for signal only 1 thread goes from 1 -> 2 whereas with
>> broadcast ALL threads go from 1 -> 2 but only ONE thread exits the wait()
>> function in either case.
>
>That is not the proper understanding. Whether (2) waiting on the mutex
>happens is a matter of chance: it depends on factors like how many
>processors are there, and how long is the mutex being held.

If the API behaves differently depending on the hardware core count then
frankly its a POS and shouldn't be used.

>> barriers but its not mandatory and MacOS doesn't support them.
>
>That's pretty lame; I wrote the first glibc/linux version of
>pthread_barrier_wait in 2001, now 22 years ago. Kids born then
>are graduating with degrees.

Yeah well, Apple. They tend to suffer from NIH syndrome quite badly. I guess
I'm lucky they support pthreads at all.

>Be that as it may, you can quite easily make barriers if you have
>mutexes/conditions. A program depending on pthread_barrier_wait can
>easily supply its own implementation to be ported to a system where that
>is missing.

Lifes too short.

>> SysV semaphores do a good job but the API is a mess.
>
>Good job of what? Implementing condition variables?

Gatekeeping critical code sections in seperate processes (not sure about
threads, are they thread safe?).

Re: Whats the point of pthread_cond_broadcast() ?

<87cz6dv1u6.fsf@doppelsaurus.mobileactivedefense.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17518&group=comp.unix.programmer#17518

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: rweikusat@talktalk.net (Rainer Weikusat)
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Mon, 13 Feb 2023 18:48:17 +0000
Lines: 47
Message-ID: <87cz6dv1u6.fsf@doppelsaurus.mobileactivedefense.com>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
<trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad>
<trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com>
<trvpl0$48dj$1@dont-email.me>
<87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com>
<ts0gdd$8207$1@dont-email.me> <_HPEL.638934$iU59.175216@fx14.iad>
<ts0h68$869b$1@dont-email.me> <IcQEL.127182$Ldj8.119120@fx47.iad>
<ts0jo2$8k6d$1@dont-email.me> <20230210224534.43@kylheku.com>
<ts7oku$1cncg$1@dont-email.me> <20230211213600.637@kylheku.com>
<tsad8u$1o6e6$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net uovkyKMPVsskeACyS1L3WQU8dLCusx6U/Rt9cE0HaekSr0Wfs=
Cancel-Lock: sha1:So6bk/k7gzZPLujf1NnU7aSD3eY= sha1:edcZr73VE2Rt/o1kjPF21wHNOS8=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Rainer Weikusat - Mon, 13 Feb 2023 18:48 UTC

Muttley@dastardlyhq.com writes:
> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>On 2023-02-11, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>>> On Sat, 11 Feb 2023 07:13:26 -0000 (UTC)
>>> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>>>On 2023-02-08, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>>>>> Nowhere have I read that inside pthread_cond_wait() there are 2
>>>>> possible thread states which you have to be aware of.
>>>>
>>>>No, there aren't. Condition variablews are specifically stateless (or
>>>>appear that way to the application), which make them easy to use. You
>>>
>>> I meant the *function* has 2 states:
>>> 1) Waiting on the condition variable
>>> 2) Waiting on the mutex
>>
>>Such states are not externally observable. There is literally no
>>standard API by which you can tell, and application logic isn't
>>required to handle any such states.
>
> No, but the application logic has to be different depending on whether you're
> using signal or broadcast.

One would assume that different applications use either broadcast or
signal, depending on what the intended use of the condition variable
is.

>>> It would seem that for signal only 1 thread goes from 1 -> 2 whereas with
>>> broadcast ALL threads go from 1 -> 2 but only ONE thread exits the wait()
>>> function in either case.
>>
>>That is not the proper understanding. Whether (2) waiting on the mutex
>>happens is a matter of chance: it depends on factors like how many
>>processors are there, and how long is the mutex being held.
>
> If the API behaves differently depending on the hardware core count then
> frankly its a POS and shouldn't be used.

The API doesn't behave differently. The behaviour of the implementation
depends partially on the hardware. It also depends on the available
system facilities. Eg, on Linux, there's a FUTEX_CMP_REQUEUE operation
which can be used to implement pthread_cond_broadcast more efficiently
than a naive implementation: Assuming more than one thread is sleeping
on the condition variable, a single syscall can be used to wake up n
threads (usually 1) and move the remaining ones to the queue of the
mutex would the wake up, exit kernel, test lock, enter kernel, sleep
dance.

Re: Whats the point of pthread_cond_broadcast() ?

<871qmtuykd.fsf@doppelsaurus.mobileactivedefense.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17519&group=comp.unix.programmer#17519

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: rweikusat@talktalk.net (Rainer Weikusat)
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Mon, 13 Feb 2023 19:58:58 +0000
Lines: 45
Message-ID: <871qmtuykd.fsf@doppelsaurus.mobileactivedefense.com>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
<trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad>
<trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com>
<trvpl0$48dj$1@dont-email.me>
<87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com>
<ts0gdd$8207$1@dont-email.me> <20230210222026.910@kylheku.com>
<ts7ob7$1cmdu$1@dont-email.me> <20230211213210.234@kylheku.com>
<tsacvv$1o5ka$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net 53FT2ojjhFOCOB7iavRtDwo/ibVKjQUNrgfuRd2bLzJMIJOEI=
Cancel-Lock: sha1:ybT59orgnmfb6MrTaIcbQ3R2fi4= sha1:jn/bwRvKdHO1TSTL/VieoeAtetA=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Rainer Weikusat - Mon, 13 Feb 2023 19:58 UTC

Muttley@dastardlyhq.com writes:
> On Sun, 12 Feb 2023 05:35:48 -0000 (UTC)
> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>On 2023-02-11, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>>> On Sat, 11 Feb 2023 06:30:46 -0000 (UTC)
>>> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>>>No, threads woken up by pthread_cond_broadcast do not go back to sleep;
>>>>they all return from the pthread_cond_wait function. That function call has
>>>
>>> No they don't all return, only 1 does.
>>
>>They all return, just not at the same time. Obviously, if the first
>>thread which returns does something unusual, like never unlocking the
>>mutex, then the others are stuck. That's almost certainly going to only
>>happen due to a bug.
>
> Why is it a bug? It may be a long time before the first exiting the wait
> unlocks the mutex depending on what the program does. The fact remains that
> only one thread exits the wait at a time regardless of whether you send a
> signal or broadcast. The disctinction in the behaviour is confusing and IMO
> its a poorly designed API. For a start if all you want to do is control
> threads stopping and starting without potentially having a deadlock with the
> controlling thread there's no need to use mutexes in the first place.

But that's not what a condition variable is supposed to be good for. The
idea behind that is that there's some shared state, eg, head and tail/
chain pointers for queue different threads need to work with. Hence, a
mutex is needed to serialize access to this state. The simple example is
a set of consumer threads consuming work items produced by a set of
producer threads. A producer thread does the following:

1. Lock the mutex.
2. Put a work item on the queue.
3. Unlock the mutex.
4. Broacast on the condition variable to wake up sleeping consumer
threads.

Consumer thread:

1. Lock the mutex.
2. Work to do? If so, goto 5
3. Sleep on condvar.
4. Goto 2.
5. Take work item off the queue.
6. Unlock the mutex.

Re: Whats the point of pthread_cond_broadcast() ?

<20230213183808.326@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17520&group=comp.unix.programmer#17520

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Tue, 14 Feb 2023 02:46:45 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 62
Message-ID: <20230213183808.326@kylheku.com>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
<trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad>
<trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com>
<trvpl0$48dj$1@dont-email.me>
<87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com>
<ts0gdd$8207$1@dont-email.me> <20230210222026.910@kylheku.com>
<ts7ob7$1cmdu$1@dont-email.me> <20230211213210.234@kylheku.com>
<tsacvv$1o5ka$1@dont-email.me>
Injection-Date: Tue, 14 Feb 2023 02:46:45 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="e225cf74c2b67a2de47faafce801fb73";
logging-data="2530153"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX192RcDpAFsexnNxzLds1IPU9vlC0l53URo="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:5q4c39kaTjUvL60BNY2sKb6N9ZY=
 by: Kaz Kylheku - Tue, 14 Feb 2023 02:46 UTC

On 2023-02-12, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
> On Sun, 12 Feb 2023 05:35:48 -0000 (UTC)
> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>On 2023-02-11, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>>> On Sat, 11 Feb 2023 06:30:46 -0000 (UTC)
>>> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>>>No, threads woken up by pthread_cond_broadcast do not go back to sleep;
>>>>they all return from the pthread_cond_wait function. That function call has
>>>
>>> No they don't all return, only 1 does.
>>
>>They all return, just not at the same time. Obviously, if the first
>>thread which returns does something unusual, like never unlocking the
>>mutex, then the others are stuck. That's almost certainly going to only
>>happen due to a bug.
>
> Why is it a bug? It may be a long time before the first exiting the wait
> unlocks the mutex depending on what the program does.

It may or may not.

The point is that all the threads that were waiting on the condition are
no longer waiting on it; they are waiting to return from the funtion,
which is subject to being serialized.

Like people who got their burgers from the fast food restaurant, which
are just passing through the exit door. They are not waiting for
burgers, just their turn to exit.

Now imagine there was some situation like "restaurant on fire". That
condition has to be broadcast: everyone leave! The customers stop
waiting for burgers and head for the door. They still can't pass through
it at the same time; one person emerges at a time.

> The fact remains that
> only one thread exits the wait at a time regardless of whether you send a
> signal or broadcast.

The fact remains that only one person leaves the burger joint, whether
or not you sound a fire alarm telling them all to leave, or whether
a single burger order was just filled, and the other customers are still
waiting for theirs.

> The disctinction in the behaviour is confusing and IMO
> its a poorly designed API.

Not to anyone who understands burger joints, doors and fire alarms.

> For a start if all you want to do is control
> threads stopping and starting without potentially having a deadlock with the
> controlling thread there's no need to use mutexes in the first place.

Controlling the starting and stopping of another thread is fraught with
race conditions. Pretty much only two kinds of applications do this:
debuggers, and stop-the-world garbage collectors.

You cannot use that for everyday synchronization.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Whats the point of pthread_cond_broadcast() ?

<20230213184706.817@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17521&group=comp.unix.programmer#17521

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Tue, 14 Feb 2023 03:06:05 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 93
Message-ID: <20230213184706.817@kylheku.com>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
<trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad>
<trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com>
<trvpl0$48dj$1@dont-email.me>
<87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com>
<ts0gdd$8207$1@dont-email.me> <_HPEL.638934$iU59.175216@fx14.iad>
<ts0h68$869b$1@dont-email.me> <IcQEL.127182$Ldj8.119120@fx47.iad>
<ts0jo2$8k6d$1@dont-email.me> <20230210224534.43@kylheku.com>
<ts7oku$1cncg$1@dont-email.me> <20230211213600.637@kylheku.com>
<tsad8u$1o6e6$1@dont-email.me>
Injection-Date: Tue, 14 Feb 2023 03:06:05 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="e225cf74c2b67a2de47faafce801fb73";
logging-data="2530153"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/QLy1+voBEQbe3I9m0TAVb4qOkjxzfKwM="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:Ajwp6vpcBFZuHKUN6VtLMkf0B7E=
 by: Kaz Kylheku - Tue, 14 Feb 2023 03:06 UTC

On 2023-02-12, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
> On Sun, 12 Feb 2023 06:27:07 -0000 (UTC)
> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>On 2023-02-11, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>>> On Sat, 11 Feb 2023 07:13:26 -0000 (UTC)
>>> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>>>On 2023-02-08, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>>>>> Nowhere have I read that inside pthread_cond_wait() there are 2
>>>>> possible thread states which you have to be aware of.
>>>>
>>>>No, there aren't. Condition variablews are specifically stateless (or
>>>>appear that way to the application), which make them easy to use. You
>>>
>>> I meant the *function* has 2 states:
>>> 1) Waiting on the condition variable
>>> 2) Waiting on the mutex
>>
>>Such states are not externally observable. There is literally no
>>standard API by which you can tell, and application logic isn't
>>required to handle any such states.
>
> No, but the application logic has to be different depending on whether you're
> using signal or broadcast.

Umm, not really. Mostly, you just have to avoid using signal when the
logic calls for brodcast, and use broadcast.

If you need broadcast, but insist on using signal, then your application
logic has to be different --- because the code as-is will not work with
signal. You have to somehow emulate the behavior of broadcast.

Rainer W. pointed this out in a reasponse to the api_ready example:
there is a way to code it without broadcast with additional logic,
involving signal being called multiple times.

We can work this back into the burger joint example. Signal is used
to indicate that a customer's order is ready. If the place is on fire,
that situation is broadcast, so everyone leaves.

Suppose you don't broadcast the "fire" signal, but still want to get
everyone to leave? You can do it like this: signal one person to leave
the building. When they exit, /they/ turn around and signal again,
causing the next person inside to exit. This repeats until the last
person leaves. When the last person signals into the building, there is
nobody left there so the signal is ignored. (If the building maitains a
count how of many people are in it, this wasteful last signal can be
avoided.)

This is simply a simulation of broadcast, which distributes the
responsibility among the threads.

>>> It would seem that for signal only 1 thread goes from 1 -> 2 whereas with
>>> broadcast ALL threads go from 1 -> 2 but only ONE thread exits the wait()
>>> function in either case.
>>
>>That is not the proper understanding. Whether (2) waiting on the mutex
>>happens is a matter of chance: it depends on factors like how many
>>processors are there, and how long is the mutex being held.
>
> If the API behaves differently depending on the hardware core count then
> frankly its a POS and shouldn't be used.

Anything with threads behaves differently based on OS platform and
its scheduling, number of cores, system load, tuning parameters, CPU
model, ...

If the application has any dependency on the execution order of pieces
of logic on different threads, then it has to ensure that somehow;
otherwise it's just depending on that by fluke.

It's possible for mutexes and ocnditions to provide stronger guarantees
about certain behaviors. For instance we can have Hoare semantics,
whereby the signaler atomically transfers a waiting thread into the
mutex, and is then guaranteed to immediately get the mutex back when
that thread is done (no matter who else wants the mutex in the
meanwhile). Hoare's semantics make it easier to prove the correctness of
some programs --- but are awful for implementing the stuff on a modern
multiprocessor, in terms of performance.

There is a tradeoff between enforcing orders---who gets the mutex in
what order, and what not---and performance. Enforcing orders introduces
overhead. Potentially huge overhead. Because you run into situations
like, for instance, a thread currently running on a processor being
primed and ready to seize a mutex, do something quick and release it,
but being prevented from doing that because some other thread is
supposed to get that mutex first. Wasteful context switching occurs.
The threads involved execute hundreds, or thousands of unnecessary
instructions.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Whats the point of pthread_cond_broadcast() ?

<tsfiuf$2fh9n$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17522&group=comp.unix.programmer#17522

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Muttley@dastardlyhq.com
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Tue, 14 Feb 2023 09:06:23 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <tsfiuf$2fh9n$1@dont-email.me>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
<trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad>
<trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com>
<trvpl0$48dj$1@dont-email.me>
<87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com>
<ts0gdd$8207$1@dont-email.me> <20230210222026.910@kylheku.com>
<ts7ob7$1cmdu$1@dont-email.me> <20230211213210.234@kylheku.com>
<tsacvv$1o5ka$1@dont-email.me>
<20230213183808.326@kylheku.com>
Injection-Date: Tue, 14 Feb 2023 09:06:23 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="70b9c607fdfba4bfa0ee039802772360";
logging-data="2606391"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18v9rbFXOzk4Hi8RNabqX9A"
Cancel-Lock: sha1:+rztLAPRaDVz6lVD1uiHMZKWhgc=
 by: Muttley@dastardlyhq.com - Tue, 14 Feb 2023 09:06 UTC

On Tue, 14 Feb 2023 02:46:45 -0000 (UTC)
Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>> For a start if all you want to do is control
>> threads stopping and starting without potentially having a deadlock with the
>> controlling thread there's no need to use mutexes in the first place.
>
>Controlling the starting and stopping of another thread is fraught with
>race conditions. Pretty much only two kinds of applications do this:
>debuggers, and stop-the-world garbage collectors.
>
>You cannot use that for everyday synchronization.

Nonsense. What do you think spinlocks are for?

Re: Whats the point of pthread_cond_broadcast() ?

<20230215165007.67@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17523&group=comp.unix.programmer#17523

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.programmer
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Date: Thu, 16 Feb 2023 00:53:46 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <20230215165007.67@kylheku.com>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
<trts7n$3q3jn$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad>
<trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com>
<trvpl0$48dj$1@dont-email.me>
<87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com>
<ts0gdd$8207$1@dont-email.me> <20230210222026.910@kylheku.com>
<ts7ob7$1cmdu$1@dont-email.me> <20230211213210.234@kylheku.com>
<tsacvv$1o5ka$1@dont-email.me> <20230213183808.326@kylheku.com>
<tsfiuf$2fh9n$1@dont-email.me>
Injection-Date: Thu, 16 Feb 2023 00:53:46 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="2fdb9deee64f6a9b7f2258210ad61389";
logging-data="3192058"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//LsNjNOK9w0VufOLsBxKLfeyeqbDZO7g="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:mt4vSG0kDNPOtkgzKAPwaqlEFdg=
 by: Kaz Kylheku - Thu, 16 Feb 2023 00:53 UTC

On 2023-02-14, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
> On Tue, 14 Feb 2023 02:46:45 -0000 (UTC)
> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>> For a start if all you want to do is control
>>> threads stopping and starting without potentially having a deadlock with the
>>> controlling thread there's no need to use mutexes in the first place.
>>
>>Controlling the starting and stopping of another thread is fraught with
>>race conditions. Pretty much only two kinds of applications do this:
>>debuggers, and stop-the-world garbage collectors.
>>
>>You cannot use that for everyday synchronization.
>
> Nonsense. What do you think spinlocks are for?

Mainly, I think that spinlocks are not an example of one thread
controlling (stopping, starting) the execution of another, so they
are unrelated to my remarks.

Spinlocks are a kind of mutex. They are acquired and released, and
understood to be held by a thread (or other context).

Deadlocks are possible with spinlocks, just as with mutexes.
For instance, two threads both try to acquire a pair of spinlocks,
but in opposite order.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Whats the point of pthread_cond_broadcast() ?

<72hHL.102430$OD18.101196@fx08.iad>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17524&group=comp.unix.programmer#17524

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx08.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: scott@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: Whats the point of pthread_cond_broadcast() ?
Newsgroups: comp.unix.programmer
References: <trqgmc$312l3$1@dont-email.me> <y3vEL.624380$iU59.162684@fx14.iad> <trtviq$3qo84$1@dont-email.me> <20230207110711.41@kylheku.com> <trvpl0$48dj$1@dont-email.me> <87k00s6w9c.fsf@doppelsaurus.mobileactivedefense.com> <ts0gdd$8207$1@dont-email.me> <20230210222026.910@kylheku.com> <ts7ob7$1cmdu$1@dont-email.me> <20230211213210.234@kylheku.com> <tsacvv$1o5ka$1@dont-email.me> <20230213183808.326@kylheku.com> <tsfiuf$2fh9n$1@dont-email.me> <20230215165007.67@kylheku.com>
Lines: 158
Message-ID: <72hHL.102430$OD18.101196@fx08.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Thu, 16 Feb 2023 03:03:31 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Thu, 16 Feb 2023 03:03:31 GMT
X-Received-Bytes: 5605
 by: Scott Lurndal - Thu, 16 Feb 2023 03:03 UTC

Kaz Kylheku <864-117-4973@kylheku.com> writes:
>On 2023-02-14, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>> On Tue, 14 Feb 2023 02:46:45 -0000 (UTC)
>> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>>> For a start if all you want to do is control
>>>> threads stopping and starting without potentially having a deadlock with the
>>>> controlling thread there's no need to use mutexes in the first place.
>>>
>>>Controlling the starting and stopping of another thread is fraught with
>>>race conditions. Pretty much only two kinds of applications do this:
>>>debuggers, and stop-the-world garbage collectors.
>>>
>>>You cannot use that for everyday synchronization.
>>
>> Nonsense. What do you think spinlocks are for?
>
>Mainly, I think that spinlocks are not an example of one thread
>controlling (stopping, starting) the execution of another, so they
>are unrelated to my remarks.
>
>Spinlocks are a kind of mutex. They are acquired and released, and
>understood to be held by a thread (or other context).
>
>Deadlocks are possible with spinlocks, just as with mutexes.
>For instance, two threads both try to acquire a pair of spinlocks,
>but in opposite order.

Although to be fair, you can use a spinlock as a sort of condition
variable in cases where:
1) there will only be one waiter and you want signal semantics
2) there may be multiple waiters and you want broadcast semantics
3) you don't care that a core will be spinning while waiting.

More suitable for coordinating between supervisory software
on an SMP processor (i.e. a bare metal hypervisor).

/**
* Initialize the condition variable to a unsignalled state.
*/
inline void
c_condition::init(void)
{ condition = 0;
timedout = 0;
}

/**
* Wait for condition state to be signalled. The waiter is awakened by
* the c_condition::signal() function setting the c_condition::condition
* value to 1.
*/
inline void
c_condition::wait(void)
{ __asm__ __volatile__ (
"\n1:\t"
"testl $0xffffffff, %0\n\t"
"jnz 2f\n\t"
"xorq %%rcx, %%rcx\n\t"
"xorq %%rdx, %%rdx\n\t"
"leaq %0, %%rax\n\t"
"monitor\n\t"
"testl $0xffffffff, %0\n\t"
"jnz 2f\n\t"
"xorq %%rax, %%rax\n\t"
"mwait\n\t"
"jmp 1b\n\t"
"2:\n\t"
"movl $0, %0\n\t"
:
: "m"(condition)
: "memory", "ax", "cx", "dx");
}

/**
* Signal condition. Implemented by setting the condition value to 1.
*/
inline void
c_condition::signal(void)
{ __asm__ __volatile__ ("movl $1, %0": "=m" (condition): : "memory");
}

/**
* Condition variable with timeout semantics. Wait for the condition
* to be signalled, or a specified timeout to elapse. Returns 'true'
* if the timeout fired.
*
* @param ns The number of nanoseconds to wait.
* @returns true if the wait was satisfied, false if the timer fired.
*/
bool
c_condition::timed_wait(uint64 ns)
{ timedout = 0;
c_timer *to = new c_timer(c_timer::ONESHOT,
c_core::get_current()->get_current_time() + ns, this);

wait();

delete to;
return (timedout == 0);
}

/**
* Callback function for condition variable timer expiration.
*
* @param arg Callback argument, unused.
*/
void
c_condition::timer_callback(void *arg)
{ timedout = 1;
signal();
}

/**
* Wait for condition state to be signalled. The waiter is awakened by
* the c_condition::signal() function setting the c_condition::condition
* value to 1 and intermitantly it wakes up and processes any pending defered
* tasks.
*/
void
c_condition::wait_run_defer(void)
{ c_core *cp = c_core::get_current();

while (condition == 0) {
cp->delay(MICROSECONDS(1));
// Handle deferred work
cp->get_dispatcher()->intercept();
}
condition = 0;
}

/**
* Condition variable with timeout semantics. Wait for the condition
* to be signalled, or a specified timeout to elapse. This function
* intermittently wakes up and processes any pending deferred tasks.
*
* @param ns The number of nanoseconds to wait.
* @returns true if the wait was satisfied, false if the timer fired.
*/
bool
c_condition::timed_wait_run_defer(uint64 ns)
{ timedout = 0;
c_timer *to = new c_timer(c_timer::ONESHOT,
c_core::get_current()->get_current_time() + ns, this);

wait_run_defer();

delete to;
return (timedout == 0);
}

/* vim: sw=4 sts=4 sta ts=8:
*/


devel / comp.unix.programmer / Re: Whats the point of pthread_cond_broadcast() ?

Pages:1234
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor