public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Corinna Vinschen <vinschen@redhat.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 17:59:53 +0100	[thread overview]
Message-ID: <ZVOniSk6JYImu+St@calimero.vinschen.de> (raw)
In-Reply-To: <6858244.H3tkzssBUc@nimes>

On Nov 14 12:52, Bruno Haible wrote:
> Corinna Vinschen wrote:
> > > 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.
> 
> This paragraph is not directed at the libc implementor; it is directed at
> the application programmer. Because
>   - Otherwise the phrase "may be wanted in a multi-threaded program"
>     would not make sense.
>   - The goals "1. A single per-process sequence of pseudo-random numbers"
>     and "2. A different sequence of pseudo-random numbers for each thread"
>     cannot be achieved at the same time, since rand() takes no argument.
> 
> > 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.
> 
> It must mean rand_r(), because rand() cannot satisfy both goals at the
> same time.
> 
> > 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.
> 
> The sense is reproducibility across threads. The standards comittee
> probably wanted as little thread-specific verbiage at this place.
> 
> The two sentences added in ISO C 11 mean that the application needs to
> sync their threads by itself, yes. But that's not uncommon: Every use of,
> say, a global linked-list or hash table object accessed by multiple threads
> needs the same precaution.

Ok, I pushed a Cygwin-only patch for now, which calls Cygwin's random(3)
functions to implement rand(3), just as GLibC does it.

Since that's entirely inside the Cygwin codebase, other newlib targets
are not affected right now.  They still have a rand(3) PRNG per execution
unit using either struct _reent, or the thread_local implementation.


Corinna


  reply	other threads:[~2023-11-14 16:59 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
2023-11-14 11:52             ` Bruno Haible
2023-11-14 16:59               ` Corinna Vinschen [this message]
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=ZVOniSk6JYImu+St@calimero.vinschen.de \
    --to=vinschen@redhat.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).