public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Corinna Vinschen <corinna-cygwin@cygwin.com>
To: Bruno Haible <bruno@clisp.org>
Cc: newlib@sourceware.org, cygwin@cygwin.com
Subject: Re: rand is not ISO C compliant in Cygwin
Date: Tue, 14 Nov 2023 11:11:01 +0100	[thread overview]
Message-ID: <ZVNHtZ+US0LFcfY3@calimero.vinschen.de> (raw)
In-Reply-To: <4205183.RD5H4TdPZm@nimes>

Hi Bruno,

On Nov 13 22:33, Bruno Haible via Cygwin wrote:
> Corinna Vinschen wrote:
> > https://pubs.opengroup.org/onlinepubs/9699919799/functions/rand.html
> > [...]
> >   With regard to rand(), there are two different behaviors that may be
> >   wanted in a multi-threaded program:
> > 
> >   1. A single per-process sequence of pseudo-random numbers that is
> >      shared by all threads that call rand()
> > 
> >   2. A different sequence of pseudo-random numbers for each thread that
> >      calls rand()
> >     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 
> This paragraph continues after the two items:
>    "This is provided by the modified thread-safe function based on whether
>     the seed value is global to the entire process or local to each thread."
> 
> My understanding of this paragraph is:
>   - If an application wants 1., they can use rand_r with SEED pointing
>     to a global variable.
>   - If an application wants 2., they can use rand_r with SEED pointing
>     to a per-thread variable.

The problem I have with bringing rand_r() into the picture at this point
is two-fold:

- The paragraph explicitely states "With regard to rand() ..."

- rand_r() is obsolescent and may be removed in a future version.

The rational section is entirely dedicated to the base functions
rand()/srand() and doesn't mention rand_r() even once.  I don't see
that the vague expression "the modified thread-safe function" is really
meant to be rand_r(), or rather rand() after an implementation decides
to make rand() thread-safe.

> > I read this as the newlib technique being one way of correctly
> > implementing rand/srand, no?
> 
> I don't think so. The critical sentence is the one with
> "subsequent calls to rand".

I see what you mean.  However, what sense is there in providing a global
state, while at the same time rand() doesn't need to be thread-safe.  In
the end, if you call srand() once and then run rand() in concurrent
threads, the implementation has no control over the sequences generated
per-thread, unless your application threads will sync the calls explicitely.

We have a potential patch to align rand/srand to your interpretation,
at least for Cygwin if nobody else in the newlib community chimes in.
It's just that, personally, I'm not yet convinced that this is the only
possible interpretation.

Sigh... yet another case of unnecessary vagueness in the standards...


Thanks,
Corinna

  parent reply	other threads:[~2023-11-14 10:11 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-10 20:19 Bruno Haible
2023-11-10 21:39 ` Norton Allen
2023-11-10 22:27   ` Bruno Haible
2023-11-11 16:50     ` Allen, Norton T.
2023-11-11 18:25       ` René Berber
2023-11-11 20:18         ` Allen, Norton T.
2023-11-13 14:17 ` Corinna Vinschen
2023-11-13 14:25   ` Bruno Haible
2023-11-13 14:38     ` Corinna Vinschen
2023-11-13 16:21       ` Corinna Vinschen
2023-11-13 16:44         ` [EXTERNAL] " Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2023-11-13 21:33         ` Bruno Haible
2023-11-13 22:14           ` Glenn Strauss
2023-11-14 10:20             ` Corinna Vinschen
2023-11-14 10:11           ` Corinna Vinschen [this message]
2023-11-14 11:52             ` Bruno Haible
2023-11-14 16:59               ` Corinna Vinschen
2023-11-14 18:06                 ` Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZVNHtZ+US0LFcfY3@calimero.vinschen.de \
    --to=corinna-cygwin@cygwin.com \
    --cc=bruno@clisp.org \
    --cc=cygwin@cygwin.com \
    --cc=newlib@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).