rand is not ISO C compliant in Cygwin
Bruno Haible
bruno@clisp.org
Fri Nov 10 20:19:51 GMT 2023
ISO C 23 § 7.24.2.1 and 7.24.2.2 document how rand() and srand() are
expected to behave. In particular:
"The srand function uses the argument as a seed for a new sequence
of pseudo-random numbers to be returned by subsequent calls to rand.
If srand is then called with the same seed value, the sequence of
pseudo-random numbers shall be repeated. ...
The srand function is not required to avoid data races with other
calls to pseudo-random sequence generation functions. ..."
The two attached programs call srand() in one thread and then rand()
in another thread. There is no data race, since the second thread
is only created after the call to srand() has returned. The behaviour
in Cygwin is that the values in the second thread ignore the srand()
call done in the first thread.
How to reproduce the bug:
$ x86_64-pc-cygwin-gcc -Wall rand-in-posix-thread.c
$ ./a
or
$ x86_64-pc-cygwin-gcc -Wall rand-in-isoc-thread.c
$ ./a
Expected output:
Value from main thread: 1583559764
Value from separate thread: 1583559764
Actual output:
Value from main thread: 1583559764
Value from separate thread: 1481765933
-------------- next part --------------
A non-text attachment was scrubbed...
Name: rand-in-posix-thread.c
Type: text/x-csrc
Size: 482 bytes
Desc: not available
URL: <https://cygwin.com/pipermail/cygwin/attachments/20231110/2e2f1d32/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: rand-in-isoc-thread.c
Type: text/x-csrc
Size: 483 bytes
Desc: not available
URL: <https://cygwin.com/pipermail/cygwin/attachments/20231110/2e2f1d32/attachment-0001.bin>
More information about the Cygwin
mailing list