Re: rand is not ISO C compliant in Cygwin

Corinna Vinschen <[email protected]>
Newsgroups gmane.comp.lib.newlib,gmane.os.cygwin
Message-ID <[email protected]>
On Nov 13 15:38, Corinna Vinschen wrote:
> On Nov 13 15:25, Bruno Haible wrote:
> > Corinna Vinschen wrote:
> > > The rand() function would still not use locking but AFAICS that's
> > > not actually required by POSIX or ISO C.
> > 
> > Correct. Those who want an MT-safe rand-like function need to use random(),
> > not rand().

I took a look into POSIX and I'm a bit puzzled now.  From
https://pubs.opengroup.org/onlinepubs/9699919799/functions/rand.html

RATIONAL

  The ISO C standard rand() and srand() functions allow per-process
                                                  ^^^^^ (not requires)

  pseudo-random streams shared by all threads. Those two functions need
  not change, but there has to be mutual-exclusion that prevents
  interference between two threads concurrently accessing the random
  number generator.

Ok, so, *iff* rand/srand share per-process state, then they have to
use locking to prevent MT interference.  POSIX continues:

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

I read this as the newlib technique being one way of correctly
implementing rand/srand, no?


Corinna
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.