From: Corinna Vinschen <vinschen@redhat.com>
To: Glenn Strauss <gs-cygwin.com@gluelogic.com>
Cc: Bruno Haible <bruno@clisp.org>, newlib@sourceware.org, cygwin@cygwin.com
Subject: Re: rand is not ISO C compliant in Cygwin
Date: Tue, 14 Nov 2023 11:20:24 +0100 [thread overview]
Message-ID: <ZVNJ6MRORt6uxq78@calimero.vinschen.de> (raw)
In-Reply-To: <ZVKfuuI8rryAwszD@xps13>
On Nov 13 17:14, Glenn Strauss wrote:
> On Mon, Nov 13, 2023 at 10:33:48PM +0100, Bruno Haible via Cygwin wrote:
> > POSIX does not have these two sentences, but instead has:
> >
> > "The rand() function need not be thread-safe."
>
> I read the above as requiring *reentrancy*, but not *thread-safety*.
>
> If multiple threads are accessing rand() and rand() accesses global
> state, the global state should not get corrupted; the sequence
> generated by rand() should remain intact. Since thread-safety is not
> guaranteed, is it theoretically possible that multiple threads enter
> rand() at nearly the same time and both get the *same* random value in
> the sequence. (Whether or not that is undesirable behavior is
> application-specific.) A mutex can avoid this theoretical duplication,
> as can using thread-local state (with difference seeds) instead of
> global state. If the seed value is the same in multiple threads using
> thread-local state, the sequence of random values generated in each
> thread will be repeated in each thread. This may be surprising behavior
> to some when srand() is called, then multiple threads spawned, and each
> thread subsequently gets the same sequence of values from rand().
That's a good common sense argument for changing rand() to use a global
state.
Corinna
next prev parent reply other threads:[~2023-11-14 10:20 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 [this message]
2023-11-14 10:11 ` Corinna Vinschen
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=ZVNJ6MRORt6uxq78@calimero.vinschen.de \
--to=vinschen@redhat.com \
--cc=bruno@clisp.org \
--cc=cygwin@cygwin.com \
--cc=gs-cygwin.com@gluelogic.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).