Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

To be is to program.


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

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

Pages:12
Whats the point of pthread_cond_broadcast() ?

<trqgmc$312l3$1@dont-email.me>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3022&group=comp.unix.programmer#3022

  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: Whats the point of pthread_cond_broadcast() ?
Date: Mon, 6 Feb 2023 09:19:08 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 6
Message-ID: <trqgmc$312l3$1@dont-email.me>
Injection-Date: Mon, 6 Feb 2023 09:19:08 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="c75cc9966003dbf68d05998cdd4ea9ef";
logging-data="3181219"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19kagpfWat2vYb+Y+XnTJJW"
Cancel-Lock: sha1:Vrxq0k4IFHsp7MtNK8JTa1A8QOw=
 by: Muttley@dastardlyhq.com - Mon, 6 Feb 2023 09:19 UTC

Even though I've been doing threads programming for years I've always
avoided this function call because I never understood the point of it
with condition variables. Ie: because all threads contend to lock the
associated mutex only one actually exits the wait call anyway, so whats
the point of using this instead of cond_signal?

Re: Whats the point of pthread_cond_broadcast() ?

<63e0db76$0$3079$426a74cc@news.free.fr>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3023&group=comp.unix.programmer#3023

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!news.nntp4.net!news.gegeweb.eu!gegeweb.org!usenet-fr.net!proxad.net!feeder1-2.proxad.net!cleanfeed2-b.proxad.net!nnrp2-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>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=iso-8859-1
Date: 06 Feb 2023 10:50:30 GMT
Lines: 11
Message-ID: <63e0db76$0$3079$426a74cc@news.free.fr>
Organization: Guest of ProXad - France
NNTP-Posting-Date: 06 Feb 2023 11:50:30 CET
NNTP-Posting-Host: 129.199.129.80
X-Trace: 1675680630 news-2.free.fr 3079 129.199.129.80:35710
X-Complaints-To: abuse@proxad.net
 by: Nicolas George - Mon, 6 Feb 2023 10:50 UTC

Muttley@dastardlyhq.com, dans le message <trqgmc$312l3$1@dont-email.me>,
a écrit :
> Even though I've been doing threads programming for years I've always
> avoided this function call because I never understood the point of it
> with condition variables. Ie: because all threads contend to lock the
> associated mutex only one actually exits the wait call anyway, so whats
> the point of using this instead of cond_signal?

An example of use:

https://git.ffmpeg.org/gitweb/ffmpeg.git/blob/HEAD:/libavcodec/frame_thread_encoder.c

Re: Whats the point of pthread_cond_broadcast() ?

<trqn7r$3278q$1@dont-email.me>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3024&group=comp.unix.programmer#3024

  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: Mon, 6 Feb 2023 11:10:52 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <trqn7r$3278q$1@dont-email.me>
References: <trqgmc$312l3$1@dont-email.me> <63e0db76$0$3079$426a74cc@news.free.fr>
Injection-Date: Mon, 6 Feb 2023 11:10:52 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="c75cc9966003dbf68d05998cdd4ea9ef";
logging-data="3218714"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18RfS+n4/KCsd0avYmewCe2"
Cancel-Lock: sha1:YNJcFK3biThJPDTTu21JLGx3MhI=
 by: Muttley@dastardlyhq.com - Mon, 6 Feb 2023 11:10 UTC

On 06 Feb 2023 10:50:30 GMT
Nicolas George <nicolas$george@salle-s.org> wrote:
>Muttley@dastardlyhq.com, dans le message <trqgmc$312l3$1@dont-email.me>,
> a �crit�:
>> Even though I've been doing threads programming for years I've always
>> avoided this function call because I never understood the point of it
>> with condition variables. Ie: because all threads contend to lock the
>> associated mutex only one actually exits the wait call anyway, so whats
>> the point of using this instead of cond_signal?
>
>An example of use:
>
>https://git.ffmpeg.org/gitweb/ffmpeg.git/blob/HEAD:/libavcodec/frame_thread_enc
>oder.c

Far too complex to be helpful.

Re: Whats the point of pthread_cond_broadcast() ?

<63e0f27c$0$22262$426a74cc@news.free.fr>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3025&group=comp.unix.programmer#3025

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!cleanfeed3-b.proxad.net!nnrp4-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> <63e0db76$0$3079$426a74cc@news.free.fr> <trqn7r$3278q$1@dont-email.me>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=iso-8859-1
Date: 06 Feb 2023 12:28:44 GMT
Lines: 6
Message-ID: <63e0f27c$0$22262$426a74cc@news.free.fr>
Organization: Guest of ProXad - France
NNTP-Posting-Date: 06 Feb 2023 13:28:44 CET
NNTP-Posting-Host: 129.199.129.80
X-Trace: 1675686524 news-2.free.fr 22262 129.199.129.80:42836
X-Complaints-To: abuse@proxad.net
 by: Nicolas George - Mon, 6 Feb 2023 12:28 UTC

Muttley@dastardlyhq.com, dans le message <trqn7r$3278q$1@dont-email.me>,
a écrit :
> Far too complex to be helpful.

You do not need to read all of it to see why the function if used. But fine,
suit yourself.

Re: Whats the point of pthread_cond_broadcast() ?

<trr7nn$355me$1@dont-email.me>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3026&group=comp.unix.programmer#3026

  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: Mon, 6 Feb 2023 15:52:23 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <trr7nn$355me$1@dont-email.me>
References: <trqgmc$312l3$1@dont-email.me> <63e0db76$0$3079$426a74cc@news.free.fr> <trqn7r$3278q$1@dont-email.me> <63e0f27c$0$22262$426a74cc@news.free.fr>
Injection-Date: Mon, 6 Feb 2023 15:52:23 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="c75cc9966003dbf68d05998cdd4ea9ef";
logging-data="3315406"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//YecpkYX3zcF+03w+lk3U"
Cancel-Lock: sha1:x+PInH+TMtzVcXhrigNT+nhnzKo=
 by: Muttley@dastardlyhq.com - Mon, 6 Feb 2023 15:52 UTC

On 06 Feb 2023 12:28:44 GMT
Nicolas George <nicolas$george@salle-s.org> wrote:
>Muttley@dastardlyhq.com, dans le message <trqn7r$3278q$1@dont-email.me>,
> a �crit�:
>> Far too complex to be helpful.
>
>You do not need to read all of it to see why the function if used. But fine,
>suit yourself.

I read it. Its no help. As far as I can see it could use signal instead of
broadcast.

Re: Whats the point of pthread_cond_broadcast() ?

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

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3027&group=comp.unix.programmer#3027

  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, 06 Feb 2023 15:53:20 +0000
Lines: 12
Message-ID: <87zg9qoknj.fsf@doppelsaurus.mobileactivedefense.com>
References: <trqgmc$312l3$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net WMwXrJTh0dxu8cEsoImvnwyit14eTMWfQt0u/d6fVLq44Buxw=
Cancel-Lock: sha1:Gq3A3iomRJ010HCXfZ+NR5b3JpU= sha1:962sYT4FCum+7rcE2obnS1FupSE=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Rainer Weikusat - Mon, 6 Feb 2023 15:53 UTC

Muttley@dastardlyhq.com writes:
> Even though I've been doing threads programming for years I've always
> avoided this function call because I never understood the point of it
> with condition variables. Ie: because all threads contend to lock the
> associated mutex only one actually exits the wait call anyway, so whats
> the point of using this instead of cond_signal?

I've yet to encounter a situation where this is useful myself. SUS
names, single-producer/ multiple consumers, writer releases a rw lock,
and two-phase commit as examples. OTOH, it's unclear (to me at least)
why one shouldn't use signal to wake up one thread which could resignal
in case there's more work to do than it plans to handle.

Re: Whats the point of pthread_cond_broadcast() ?

<20230206142250.260@kylheku.com>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3028&group=comp.unix.programmer#3028

  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: Mon, 6 Feb 2023 22:30:39 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <20230206142250.260@kylheku.com>
References: <trqgmc$312l3$1@dont-email.me>
Injection-Date: Mon, 6 Feb 2023 22:30:39 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="7877620feb46a40488176087f454a529";
logging-data="3404859"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19DubbnCIfnpFmlKIPlWxRwOWWnCjJRsUI="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:F46hrrEtaGO1d2FPCCC1TOs4JVY=
 by: Kaz Kylheku - Mon, 6 Feb 2023 22:30 UTC

On 2023-02-06, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
> Even though I've been doing threads programming for years I've always
> avoided this function call because I never understood the point of it
> with condition variables. Ie: because all threads contend to lock the
> associated mutex only one actually exits the wait call anyway, so whats
> the point of using this instead of cond_signal?

You should always reach for pthread_cond_broadcast in initial designs
and implementaitons, because it will more easily assure correctness.

In a correct program, every pthread_cond_signal can be replaced with
pthread_cond_broadcast without sacrificing correctness. The reverse
isn't true.

pthread_cond_signal is an optimization for the case when you are
absolutely sure that the program will still behave correctly if only
at most one thread is woken.

(I seem to recall that no function guarantees that no more than one
thread will wake up; programs must be prepared for spurious wakeups.)

There are situations in which making some Boolean condition true means
that any number of threads are unblocked, not only one. For instance
a ready-wait: one or more threads are blocked waiting for some API
to become ready for service.

A situation where you need at most one thread to wake up is when
implementing a counting semaphore.

--
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() ?

<20230206143106.679@kylheku.com>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3029&group=comp.unix.programmer#3029

  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: Mon, 6 Feb 2023 22:35:07 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <20230206143106.679@kylheku.com>
References: <trqgmc$312l3$1@dont-email.me>
<87zg9qoknj.fsf@doppelsaurus.mobileactivedefense.com>
Injection-Date: Mon, 6 Feb 2023 22:35:07 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="7877620feb46a40488176087f454a529";
logging-data="3404859"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/rPtGDMQdkErtAWe7gpoaTOAKcPqt/BW4="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:XXamtc3jmufDeaa/PKRIBv/84dQ=
 by: Kaz Kylheku - Mon, 6 Feb 2023 22:35 UTC

On 2023-02-06, Rainer Weikusat <rweikusat@talktalk.net> wrote:
> Muttley@dastardlyhq.com writes:
>> Even though I've been doing threads programming for years I've always
>> avoided this function call because I never understood the point of it
>> with condition variables. Ie: because all threads contend to lock the
>> associated mutex only one actually exits the wait call anyway, so whats
>> the point of using this instead of cond_signal?
>
> I've yet to encounter a situation where this is useful myself. SUS
> names, single-producer/ multiple consumers, writer releases a rw lock,
> and two-phase commit as examples. OTOH, it's unclear (to me at least)
> why one shouldn't use signal to wake up one thread which could resignal
> in case there's more work to do than it plans to handle.

Suppose you have a "bool api_ready = false" flag, with a condition and
mutex. Threads must wait for the API to become ready before using it:

void api_wait()
{
pthread_mutex_lock(&api_mutex);
while (!api_ready)
pthread_cond_wait(&api_cond, &api_mutex);
pthread_mutex_ulock(&api_mutex);
}

void api_ready()
{
pthread_mutex_lock(&api_mutex);
api_ready = true;
pthread_mutex_ulock(&api_mutex);
// pthread_cond_signal, or pthread_cond_broadcast?
}

How would you complete api_ready?

If you chose signal, how would you code around the fact that
only one thread was woken up to use the API?

Do you put a just-in-case pthread_cond_signal into every single API
call?

--
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() ?

<trt544$3guf1$1@dont-email.me>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3030&group=comp.unix.programmer#3030

  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, 7 Feb 2023 09:20:04 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <trt544$3guf1$1@dont-email.me>
References: <trqgmc$312l3$1@dont-email.me> <20230206142250.260@kylheku.com>
Injection-Date: Tue, 7 Feb 2023 09:20:04 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="c0f340199e48f5bbe25065e00dc96d0f";
logging-data="3701217"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX181R3ZJg9TRw519yRtSzepj"
Cancel-Lock: sha1:zxoCFtTPsDCXuhiLPZeazn++EVw=
 by: Muttley@dastardlyhq.com - Tue, 7 Feb 2023 09:20 UTC

On Mon, 6 Feb 2023 22:30:39 -0000 (UTC)
Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>On 2023-02-06, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
>> Even though I've been doing threads programming for years I've always
>> avoided this function call because I never understood the point of it
>> with condition variables. Ie: because all threads contend to lock the
>> associated mutex only one actually exits the wait call anyway, so whats
>> the point of using this instead of cond_signal?
>
>You should always reach for pthread_cond_broadcast in initial designs
>and implementaitons, because it will more easily assure correctness.
>
>In a correct program, every pthread_cond_signal can be replaced with
>pthread_cond_broadcast without sacrificing correctness. The reverse
>isn't true.
>
>pthread_cond_signal is an optimization for the case when you are
>absolutely sure that the program will still behave correctly if only
>at most one thread is woken.

But why? Regardless of which call you use only 1 waiting thread exits the
pthread_cond_wait() call so what difference does it make? The ONLY difference
I have found is that signal wakes up threads in a particular order (seems to
be creation order on MacOS) so you know which thread will exit wait() whereas
broadcast wakes them all up and a random thread exits wait(). However I can't
see how that is any kind of advantage if the threads are all executing the
same code anyway.

Re: Whats the point of pthread_cond_broadcast() ?

<63e21b41$0$31545$426a74cc@news.free.fr>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3031&group=comp.unix.programmer#3031

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!cleanfeed3-b.proxad.net!nnrp1-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> <20230206142250.260@kylheku.com> <trt544$3guf1$1@dont-email.me>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=iso-8859-1
Date: 07 Feb 2023 09:34:57 GMT
Lines: 9
Message-ID: <63e21b41$0$31545$426a74cc@news.free.fr>
Organization: Guest of ProXad - France
NNTP-Posting-Date: 07 Feb 2023 10:34:57 CET
NNTP-Posting-Host: 129.199.129.80
X-Trace: 1675762497 news-1.free.fr 31545 129.199.129.80:35118
X-Complaints-To: abuse@proxad.net
 by: Nicolas George - Tue, 7 Feb 2023 09:34 UTC

Muttley@dastardlyhq.com, dans le message <trt544$3guf1$1@dont-email.me>,
a écrit :
> But why? Regardless of which call you use only 1 waiting thread exits the
> pthread_cond_wait() call so what difference does it make?

I consider Kaz's answer to be nonsense. Signal is absolutely not just an
optimization over broadcast: they have clear semantics and are to be used in
different circumstances. The FFmpeg code I quoted gives a very good example
of these different circumstances.

Re: Whats the point of pthread_cond_broadcast() ?

<trt6bs$3h5nq$1@dont-email.me>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3032&group=comp.unix.programmer#3032

  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, 7 Feb 2023 09:41:16 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <trt6bs$3h5nq$1@dont-email.me>
References: <trqgmc$312l3$1@dont-email.me> <20230206142250.260@kylheku.com> <trt544$3guf1$1@dont-email.me> <63e21b41$0$31545$426a74cc@news.free.fr>
Injection-Date: Tue, 7 Feb 2023 09:41:16 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="c0f340199e48f5bbe25065e00dc96d0f";
logging-data="3708666"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1844bPRATTg0YQVBEAgFVyR"
Cancel-Lock: sha1:hehOlWNqpKft3ABVAAttpSTEpNE=
 by: Muttley@dastardlyhq.com - Tue, 7 Feb 2023 09:41 UTC

On 07 Feb 2023 09:34:57 GMT
Nicolas George <nicolas$george@salle-s.org> wrote:
>Muttley@dastardlyhq.com, dans le message <trt544$3guf1$1@dont-email.me>,
> a �crit�:
>> But why? Regardless of which call you use only 1 waiting thread exits the
>> pthread_cond_wait() call so what difference does it make?
>
>I consider Kaz's answer to be nonsense. Signal is absolutely not just an
>optimization over broadcast: they have clear semantics and are to be used in
>different circumstances. The FFmpeg code I quoted gives a very good example
>of these different circumstances.

How about explaining it then because I can't see it.

Re: Whats the point of pthread_cond_broadcast() ?

<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3033&group=comp.unix.programmer#3033

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news-peer.in.tum.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: Wed, 8 Feb 2023 00:54:51 +1100
Lines: 20
Message-ID: <5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
References: <trqgmc$312l3$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 hPqI6J6d49CICaUAWxH+AAkyRzKkRY7Pt5NACOQcn2vDzvkBU=
X-Orig-Path: paranoia.mcleod-schmidt.id.au!not-for-mail
Cancel-Lock: sha1:YiZ5tSGr9kxaENgoWSf6Z8LTlBM=
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: <trqgmc$312l3$1@dont-email.me>
X-Clacks-Overhead: GNU Terry Pratchett
 by: Gary R. Schmidt - Tue, 7 Feb 2023 13:54 UTC

On 06/02/2023 20:19, Muttley@dastardlyhq.com wrote:
> Even though I've been doing threads programming for years I've always
> avoided this function call because I never understood the point of it
> with condition variables. Ie: because all threads contend to lock the
> associated mutex only one actually exits the wait call anyway, so whats
> the point of using this instead of cond_signal?
>
What I was told, a couple of decades ago, was that _signal meant that
(usually/mostly/maybe/probably) only one thread waiting on that signal
would wake up, whereas _broadcast meant that *all* threads waiting on
that signal would wake. (I think it may have been by David Butenhof, I
kind of trust him. (And it may have been DEC Alpha-specific.))

So, frex, if you have multiple consumers waiting on data that is being
fed into a single queue, starting them all up when something is
available can drain the queue faster, as data may be added while they
are waiting on the queue mutex. Sort of. Worked for me. :-)

Cheers,
Gary B-)

Re: Whats the point of pthread_cond_broadcast() ?

<878rh9r09k.fsf@doppelsaurus.mobileactivedefense.com>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3034&group=comp.unix.programmer#3034

  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: Tue, 07 Feb 2023 14:57:43 +0000
Lines: 51
Message-ID: <878rh9r09k.fsf@doppelsaurus.mobileactivedefense.com>
References: <trqgmc$312l3$1@dont-email.me>
<87zg9qoknj.fsf@doppelsaurus.mobileactivedefense.com>
<20230206143106.679@kylheku.com>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net F6YJnt2uqNZXW6VnlQy5pw81WPkSvyIVKBUuAlpngLYNsSt/Y=
Cancel-Lock: sha1:eN03v/V871ibMeeGR7++XEpXE+c= sha1:2GClVIAw7ThAWwlCXY9nLCpWzuc=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Rainer Weikusat - Tue, 7 Feb 2023 14:57 UTC

Kaz Kylheku <864-117-4973@kylheku.com> writes:
> On 2023-02-06, Rainer Weikusat <rweikusat@talktalk.net> wrote:
>> Muttley@dastardlyhq.com writes:
>>> Even though I've been doing threads programming for years I've always
>>> avoided this function call because I never understood the point of it
>>> with condition variables. Ie: because all threads contend to lock the
>>> associated mutex only one actually exits the wait call anyway, so whats
>>> the point of using this instead of cond_signal?
>>
>> I've yet to encounter a situation where this is useful myself. SUS
>> names, single-producer/ multiple consumers, writer releases a rw lock,
>> and two-phase commit as examples. OTOH, it's unclear (to me at least)
>> why one shouldn't use signal to wake up one thread which could resignal
>> in case there's more work to do than it plans to handle.
>
> Suppose you have a "bool api_ready = false" flag, with a condition and
> mutex. Threads must wait for the API to become ready before using it:
>

static unsigned waiting;

>
> void api_wait()
> {

unsigned more;

> pthread_mutex_lock(&api_mutex);

if (!api_ready) {
++waiting;
do pthread_cond_wait(&api_cond, &api_mutex); while (!api_ready);
more = --waiting;
> pthread_mutex_ulock(&api_mutex);
if (more) pthread_cond_signal(&api_cond);
}
> }
>
> void api_ready()
> {
> pthread_mutex_lock(&api_mutex);
> api_ready = true;
> pthread_mutex_ulock(&api_mutex);
pthread_cond_signal(&api_cond);
>
> How would you complete api_ready?
>
> If you chose signal, how would you code around the fact that
> only one thread was woken up to use the API?

It's not particularly difficult to code this without thundering nerds.

Re: Whats the point of pthread_cond_broadcast() ?

<trts7n$3q3jn$1@dont-email.me>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3035&group=comp.unix.programmer#3035

  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, 7 Feb 2023 15:54:31 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <trts7n$3q3jn$1@dont-email.me>
References: <trqgmc$312l3$1@dont-email.me> <5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
Injection-Date: Tue, 7 Feb 2023 15:54:31 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="c0f340199e48f5bbe25065e00dc96d0f";
logging-data="4001399"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+xf+3DE8rljRBjG5Fl0aLN"
Cancel-Lock: sha1:Vekg7dir9nN7DNkI62WBo35LERw=
 by: Muttley@dastardlyhq.com - Tue, 7 Feb 2023 15:54 UTC

On Wed, 8 Feb 2023 00:54:51 +1100
"Gary R. Schmidt" <grschmidt@acm.org> wrote:
>On 06/02/2023 20:19, Muttley@dastardlyhq.com wrote:
>> Even though I've been doing threads programming for years I've always
>> avoided this function call because I never understood the point of it
>> with condition variables. Ie: because all threads contend to lock the
>> associated mutex only one actually exits the wait call anyway, so whats
>> the point of using this instead of cond_signal?
>>
>What I was told, a couple of decades ago, was that _signal meant that
>(usually/mostly/maybe/probably) only one thread waiting on that signal
>would wake up, whereas _broadcast meant that *all* threads waiting on
>that signal would wake. (I think it may have been by David Butenhof, I
>kind of trust him. (And it may have been DEC Alpha-specific.))

Yes, that is the official position and in the OS kernel all the threads
may well get woken up. However from the application program POV there's no
difference because only 1 thread exits pthread_cond_wait() per signal or
broadcast sent.

>So, frex, if you have multiple consumers waiting on data that is being
>fed into a single queue, starting them all up when something is
>available can drain the queue faster, as data may be added while they
>are waiting on the queue mutex. Sort of. Worked for me. :-)

I don't follow. For 1 condition signal/broadcast 1 thread exits a wait call.
If multiple threads exited the wait then that would be a useful paradigm
but AFAIK pthreads doesn't supply that functionality.

Re: Whats the point of pthread_cond_broadcast() ?

<y3vEL.624380$iU59.162684@fx14.iad>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3036&group=comp.unix.programmer#3036

  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!fx14.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>
Lines: 38
Message-ID: <y3vEL.624380$iU59.162684@fx14.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Tue, 07 Feb 2023 16:33:34 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Tue, 07 Feb 2023 16:33:34 GMT
X-Received-Bytes: 2757
 by: Scott Lurndal - Tue, 7 Feb 2023 16:33 UTC

Muttley@dastardlyhq.com writes:
>On Wed, 8 Feb 2023 00:54:51 +1100
>"Gary R. Schmidt" <grschmidt@acm.org> wrote:
>>On 06/02/2023 20:19, Muttley@dastardlyhq.com wrote:
>>> Even though I've been doing threads programming for years I've always
>>> avoided this function call because I never understood the point of it
>>> with condition variables. Ie: because all threads contend to lock the
>>> associated mutex only one actually exits the wait call anyway, so whats
>>> the point of using this instead of cond_signal?
>>>
>>What I was told, a couple of decades ago, was that _signal meant that
>>(usually/mostly/maybe/probably) only one thread waiting on that signal
>>would wake up, whereas _broadcast meant that *all* threads waiting on
>>that signal would wake. (I think it may have been by David Butenhof, I
>>kind of trust him. (And it may have been DEC Alpha-specific.))
>
>Yes, that is the official position and in the OS kernel all the threads
>may well get woken up. However from the application program POV there's no
>difference because only 1 thread exits pthread_cond_wait() per signal or
>broadcast sent.
>
>>So, frex, if you have multiple consumers waiting on data that is being
>>fed into a single queue, starting them all up when something is
>>available can drain the queue faster, as data may be added while they
>>are waiting on the queue mutex. Sort of. Worked for me. :-)
>
>I don't follow. For 1 condition signal/broadcast 1 thread exits a wait call.
>If multiple threads exited the wait then that would be a useful paradigm
>but AFAIK pthreads doesn't supply that functionality.

If you use broadcast, all threads waiting on the condition variable
will be made runnable. Then they will all immediately contend for
the mutex that was temporarily dropped while waiting on the event,
particularly if there are enough cores to run them all in parallel
and the scheduling constraints (e.g. thread priorities) allow the
newly runnable threads to be assigned to a physical thread/core.

This is known as a thundering herd.

Re: Whats the point of pthread_cond_broadcast() ?

<trtviq$3qo84$1@dont-email.me>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3037&group=comp.unix.programmer#3037

  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, 7 Feb 2023 16:51:38 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <trtviq$3qo84$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>
Injection-Date: Tue, 7 Feb 2023 16:51:38 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="c0f340199e48f5bbe25065e00dc96d0f";
logging-data="4022532"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Hl8iIcqTFdpP4ITa1GEaf"
Cancel-Lock: sha1:CXEMNt7gQ/Q/WYm0jujNRkP73Ig=
 by: Muttley@dastardlyhq.com - Tue, 7 Feb 2023 16:51 UTC

On Tue, 07 Feb 2023 16:33:34 GMT
scott@slp53.sl.home (Scott Lurndal) wrote:
>Muttley@dastardlyhq.com writes:
>>On Wed, 8 Feb 2023 00:54:51 +1100
>>"Gary R. Schmidt" <grschmidt@acm.org> wrote:
>>>On 06/02/2023 20:19, Muttley@dastardlyhq.com wrote:
>>>> Even though I've been doing threads programming for years I've always
>>>> avoided this function call because I never understood the point of it
>>>> with condition variables. Ie: because all threads contend to lock the
>>>> associated mutex only one actually exits the wait call anyway, so whats
>>>> the point of using this instead of cond_signal?
>>>>
>>>What I was told, a couple of decades ago, was that _signal meant that
>>>(usually/mostly/maybe/probably) only one thread waiting on that signal
>>>would wake up, whereas _broadcast meant that *all* threads waiting on
>>>that signal would wake. (I think it may have been by David Butenhof, I
>>>kind of trust him. (And it may have been DEC Alpha-specific.))
>>
>>Yes, that is the official position and in the OS kernel all the threads
>>may well get woken up. However from the application program POV there's no
>>difference because only 1 thread exits pthread_cond_wait() per signal or
>>broadcast sent.
>>
>>>So, frex, if you have multiple consumers waiting on data that is being
>>>fed into a single queue, starting them all up when something is
>>>available can drain the queue faster, as data may be added while they
>>>are waiting on the queue mutex. Sort of. Worked for me. :-)
>>
>>I don't follow. For 1 condition signal/broadcast 1 thread exits a wait call.
>>If multiple threads exited the wait then that would be a useful paradigm
>>but AFAIK pthreads doesn't supply that functionality.
>
>If you use broadcast, all threads waiting on the condition variable
>will be made runnable. Then they will all immediately contend for
>the mutex that was temporarily dropped while waiting on the event,
>particularly if there are enough cores to run them all in parallel
>and the scheduling constraints (e.g. thread priorities) allow the
>newly runnable threads to be assigned to a physical thread/core.
>
>This is known as a thundering herd.

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.

Re: Whats the point of pthread_cond_broadcast() ?

<874jrxqt61.fsf@doppelsaurus.mobileactivedefense.com>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3038&group=comp.unix.programmer#3038

  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: Tue, 07 Feb 2023 17:31:02 +0000
Lines: 47
Message-ID: <874jrxqt61.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>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net 8Bpj8dcoq8Sv2j6j5RRC4gGGrOSISy/ETi3FEgCI/3PdeG48g=
Cancel-Lock: sha1:3uXtgDxrKyi7tgvxXzBlEEXzi0A= sha1:Ui559ZqiVpzNpvKp+ZpRS+roS5w=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Rainer Weikusat - Tue, 7 Feb 2023 17:31 UTC

Muttley@dastardlyhq.com writes:
> On Tue, 07 Feb 2023 16:33:34 GMT
> scott@slp53.sl.home (Scott Lurndal) wrote:
>>Muttley@dastardlyhq.com writes:

[...]

>>>I don't follow. For 1 condition signal/broadcast 1 thread exits a wait call.
>>>If multiple threads exited the wait then that would be a useful paradigm
>>>but AFAIK pthreads doesn't supply that functionality.
>>
>>If you use broadcast, all threads waiting on the condition variable
>>will be made runnable. Then they will all immediately contend for
>>the mutex that was temporarily dropped while waiting on the event,
>>particularly if there are enough cores to run them all in parallel
>>and the scheduling constraints (e.g. thread priorities) allow the
>>newly runnable threads to be assigned to a physical thread/core.
>>
>>This is known as a thundering herd.
>
> 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.

I think that was supposed to describe a disadvantage. :-)

My guess why this interface exists would be that it's historical baggage
instinctively carried over from the UNIX kernel by people who were
accustomed to it. In order to wait for an event, a process executing
kernel code would go to sleep on a so-called sleep channel identified by
a pointer. This would typically be the address of a kernel variable
associated with the event the process wanted to wait for. This sleep
channel identifier was hashed to map it to a sleep queue and the process
would then put onto this sleep queue. At some later time, some other
code also executing in the kernel would do a wake up call using a sleep
channel identifer. This would cause all processes on the corresponding
sleep queue to be woken up. This had to be done because there was no way
to determine what event a process using a certain sleep channel
identifier was actually interested in (and possibly, because of hash
collisions in case the actual sleep channel identifier a process had
used wasn't stored anywhere).

With pthread_cond_broadcast, a condition variable can act as sleep
channel: It's possible to write a program such that it uses a single
condition variable for everything some thread might need to wait for. If
this was done, broadcast wakeup would be needed for the same reason it
was needed for UNIX sleep channels.

Re: Whats the point of pthread_cond_broadcast() ?

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

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3039&group=comp.unix.programmer#3039

  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: Tue, 07 Feb 2023 17:38:19 +0000
Lines: 28
Message-ID: <87zg9ppe9g.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>
<874jrxqt61.fsf@doppelsaurus.mobileactivedefense.com>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net FPQYabrzZao4qE2xgkgLAwCkh8WVGIjS5yerxZIYoad+Vi+4A=
Cancel-Lock: sha1:CybTXeP3h0mXq/H/oC1kIfI50/Y= sha1:+YX1C6OhyVcHsoQaYzD7imaUaL4=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Rainer Weikusat - Tue, 7 Feb 2023 17:38 UTC

Rainer Weikusat <rweikusat@talktalk.net> writes:
> Muttley@dastardlyhq.com writes:

[...]

>> I still don't see
>> what the difference and/or advantage of using broadcast vs signal is
>> to the application.

[...]

> My guess why this interface exists would be that it's historical baggage
> instinctively carried over from the UNIX kernel by people who were
> accustomed to it. In order to wait for an event, a process executing
> kernel code would go to sleep on a so-called sleep channel identified by
> a pointer.

[...]

> With pthread_cond_broadcast, a condition variable can act as sleep
> channel

[...]

Slight correction: It can act as sleep queue for any number of sleep
channels, ie, the original UNIX sleep/ wakeup mechanism can be
implemented with the help of a condition variable or an array of condition
variables sleep channel IDs are hashed onto.

Re: Whats the point of pthread_cond_broadcast() ?

<20230207104704.818@kylheku.com>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3040&group=comp.unix.programmer#3040

  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, 7 Feb 2023 18:51:13 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <20230207104704.818@kylheku.com>
References: <trqgmc$312l3$1@dont-email.me> <20230206142250.260@kylheku.com>
<trt544$3guf1$1@dont-email.me> <63e21b41$0$31545$426a74cc@news.free.fr>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 7 Feb 2023 18:51:13 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="38379bdf7bd549f83624aad3386ee912";
logging-data="4031426"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19BmKciKYRcZQw5MNyX89JPeytNg/yyBRQ="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:m4Yjh8sZZa8dRLZaTaMzVBrSYK8=
 by: Kaz Kylheku - Tue, 7 Feb 2023 18:51 UTC

On 2023-02-07, Nicolas George <nicolas$george@salle-s.org> wrote:
> Muttley@dastardlyhq.com, dans le message <trt544$3guf1$1@dont-email.me>,
> a écrit :
>> But why? Regardless of which call you use only 1 waiting thread exits the
>> pthread_cond_wait() call so what difference does it make?
>
> I consider Kaz's answer to be nonsense. Signal is absolutely not just an
> optimization over broadcast: they have clear semantics and are to be used in

I explained very clearly that it's not that kind of optimization where
we improve something, but the semantics stays the same (as in "compiler
optimization").

It definitely has different semantics; it gives you an optimization when
it is unnecessary and wasteful (or even pathologic) for more than one
thread to wake up.

--
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() ?

<20230207105619.740@kylheku.com>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3041&group=comp.unix.programmer#3041

  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, 7 Feb 2023 19:04:59 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <20230207105619.740@kylheku.com>
References: <trqgmc$312l3$1@dont-email.me>
<5q1abj-n2q.ln1@paranoia.mcleod-schmidt.id.au>
<trts7n$3q3jn$1@dont-email.me>
Injection-Date: Tue, 7 Feb 2023 19:04:59 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="38379bdf7bd549f83624aad3386ee912";
logging-data="4031426"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19yl99wk5pyYwCQFx5TBWsH5tuwcCCl5J4="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:lHHELDDYxUSlRsKM4CEjznB7ccQ=
 by: Kaz Kylheku - Tue, 7 Feb 2023 19:04 UTC

On 2023-02-07, Muttley@dastardlyhq.com <Muttley@dastardlyhq.com> wrote:
> Yes, that is the official position and in the OS kernel all the threads
> may well get woken up. However from the application program POV there's no
> difference because only 1 thread exits pthread_cond_wait() per signal or
> broadcast sent.

That is simply false. It's true that only one waiting thread at a time
can exit from the function. But that's because of the mutex.

If you broadcast the condition, all threads waiting on the condition
will wake up and contend for the mutex.

Threads contenting for the mutex proceed when the mutex is available.

Threads sleeping on the condition variable do not proceed regardless
of what is happening with the mutex.

> If multiple threads exited the wait then that would be a useful paradigm
> but AFAIK pthreads doesn't supply that functionality.

Yes it does! If you need the paradigm of multiple threads being
woken up together without having to pass serially through a mutex,
POSIX provides barriers: pthread_barrier_wait.

It is indeed useful for parallel processing.

--
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() ?

<20230207110711.41@kylheku.com>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3042&group=comp.unix.programmer#3042

  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, 7 Feb 2023 19:33:41 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 62
Message-ID: <20230207110711.41@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>
Injection-Date: Tue, 7 Feb 2023 19:33:41 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="38379bdf7bd549f83624aad3386ee912";
logging-data="4047884"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ndMJz9v2RbPCybmejHydvSkVgnJ6sNDY="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:d6NvgfuhQMoVVg6cEgCVuhruAdw=
 by: Kaz Kylheku - Tue, 7 Feb 2023 19:33 UTC

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."

Do you understand the api_ready example? Here it is again:

bool api_ready = false;
pthread_mutex_t api_ready_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t api_ready_cond = PTHREAD_COND_INITIALIZER;

// internal function: indicates API is ready
static void api_becomes_ready(void)
{
pthread_mutex_lock(&api_mutex);
api_ready = true;
pthread_mutex_unlock(&api_mutex);
pthread_cond_broadcast(&api_cond); // wake up everyone
}

void api_ready_wait(void)
{
pthread_mutex_lock(&api_mutex);
while (!api_ready)
pthread_cond_wait(&api_cond, &api_mutex);

// Yes, only one thread can be here at a time

pthread_mutex_unlock(&api_mutex);

// Multiple threads can be returning here.
}

The api_becomes_ready function is called exactly once by the API
itself to indicate that it is ready.

Muliple threads from different subsystems may have called api_ready_wait
in order to wait for this indication.

If pthread_cond_signal is used, then quite likely, only one of those
threads will wake up; the others will stay stuck forever.

You can code this up and try it.

I think the subtlety you're missing is what I commented on in the code:
although exit from pthread_cond_wait is serialized due to the mutex,
exit from the surrounding function api_ready_wait isn't; mutiple threads
can be exiting from that function. The serialization through the mutex
is just a local speedbump.

--
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() ?

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

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3043&group=comp.unix.programmer#3043

  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: Tue, 07 Feb 2023 20:51:06 +0000
Lines: 39
Message-ID: <87v8kdp5c5.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>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net 9O/hOPBSnEs8kcZvSxJ9XwfV1mw2c73sUj4ldVAKetavHn3CE=
Cancel-Lock: sha1:z9aN43AjK2iimbMKv41G02bk4rA= sha1:WDlM3OyfGfkNvtMFnnC1Zwk2UXU=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Rainer Weikusat - Tue, 7 Feb 2023 20:51 UTC

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.

For the other case (assuming 1:1 threading) n, n > 1 threads sleeping in the
kernel are woken up, exit the kernel and contend for the mutex which
will cause n - 1 threads to immediately enter the kernel again and again go to
sleep there. That's not an issue on uniprocessors because only one of
these n threads can actually be running at any given time, hence, n - 1
end up on the runqueue and exit the kernel without contention on the
mutex one-by-one.

> Do you understand the api_ready example? Here it is again:

[...]

> If pthread_cond_signal is used, then quite likely, only one of those
> threads will wake up; the others will stay stuck forever.
>
> You can code this up and try it.

As demonstrated in another posting: This can be handled by maintaing a
counter of waiting threads and having the running thread wake up the
next one if there's still a thread waiting.

Re: Whats the point of pthread_cond_broadcast() ?

<45zEL.446898$Tcw8.261380@fx10.iad>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3044&group=comp.unix.programmer#3044

  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> <87v8kdp5c5.fsf@doppelsaurus.mobileactivedefense.com>
Lines: 24
Message-ID: <45zEL.446898$Tcw8.261380@fx10.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Tue, 07 Feb 2023 21:08:16 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Tue, 07 Feb 2023 21:08:16 GMT
X-Received-Bytes: 1994
 by: Scott Lurndal - Tue, 7 Feb 2023 21:08 UTC

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.

Re: Whats the point of pthread_cond_broadcast() ?

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

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3045&group=comp.unix.programmer#3045

  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: Tue, 07 Feb 2023 21:50:42 +0000
Lines: 32
Message-ID: <87r0v1p2kt.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>
<87v8kdp5c5.fsf@doppelsaurus.mobileactivedefense.com>
<45zEL.446898$Tcw8.261380@fx10.iad>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: individual.net 4FNoK1Jl5ch7uApzZGpAtAkQ/qXF+Mg1DS3olh/nHzvNFS8VM=
Cancel-Lock: sha1:O2Ppjp35wmvDzoGMExvXqz8xp1c= sha1:OyWawy/bfi+CWk6gWpVnLpDVukM=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Rainer Weikusat - Tue, 7 Feb 2023 21:50 UTC

scott@slp53.sl.home (Scott Lurndal) writes:
> 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.

The paragraph I'm referring to obviously talks about pthread_cond_signal
("one thread has been woken up").

> 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.

That's an act of God outside of the scope of this discussion.

Re: Whats the point of pthread_cond_broadcast() ?

<63e2d765$0$24795$426a74cc@news.free.fr>

  copy mid

http://rslight.i2p/devel/article-flat.php?id=3046&group=comp.unix.programmer#3046

  copy link   Newsgroups: comp.unix.programmer
Path: i2pn2.org!i2pn.org!news.nntp4.net!news.gegeweb.eu!gegeweb.org!usenet-fr.net!proxad.net!feeder1-2.proxad.net!cleanfeed2-b.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> <20230206142250.260@kylheku.com> <trt544$3guf1$1@dont-email.me> <63e21b41$0$31545$426a74cc@news.free.fr> <trt6bs$3h5nq$1@dont-email.me>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=iso-8859-1
Date: 07 Feb 2023 22:57:42 GMT
Lines: 11
Message-ID: <63e2d765$0$24795$426a74cc@news.free.fr>
Organization: Guest of ProXad - France
NNTP-Posting-Date: 07 Feb 2023 23:57:42 CET
NNTP-Posting-Host: 129.199.129.80
X-Trace: 1675810662 news-3.free.fr 24795 129.199.129.80:48136
X-Complaints-To: abuse@proxad.net
 by: Nicolas George - Tue, 7 Feb 2023 22:57 UTC

Muttley@dastardlyhq.com, dans le message <trt6bs$3h5nq$1@dont-email.me>,
a écrit :
> How about explaining it then because I can't see it.

Consider a message queue with possibly multiple consumers.

If you add a message to the queue, you should call signal, because only one
consumer will be able to get your message.

If you mark the end of the queue, you should call broadcast, because all
consumers need to know the work is done.


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

Pages:12
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor