public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* rand is not ISO C compliant in Cygwin
@ 2023-11-10 20:19 Bruno Haible
  2023-11-10 21:39 ` Norton Allen
  2023-11-13 14:17 ` Corinna Vinschen
  0 siblings, 2 replies; 18+ messages in thread
From: Bruno Haible @ 2023-11-10 20:19 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 1175 bytes --]

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


[-- Attachment #2: rand-in-posix-thread.c --]
[-- Type: text/x-csrc, Size: 482 bytes --]

#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

static void *
rand_invocator_thread (void *arg)
{
  printf ("Value from separate thread: %d\n", rand ());
  return NULL;
}

int
main ()
{
  unsigned int seed = 19891109;

  srand (seed);
  printf ("Value from main thread:     %d\n", rand ());
  srand (seed);
  pthread_t t;
  assert (pthread_create (&t, NULL, rand_invocator_thread, NULL) == 0);
  assert (pthread_join (t, NULL) == 0);

  return 0;
}

[-- Attachment #3: rand-in-isoc-thread.c --]
[-- Type: text/x-csrc, Size: 483 bytes --]

#include <assert.h>
#include <threads.h>
#include <stdio.h>
#include <stdlib.h>

static int
rand_invocator_thread (void *arg)
{
  printf ("Value from separate thread: %d\n", rand ());
  return 0;
}

int
main ()
{
  unsigned int seed = 19891109;

  srand (seed);
  printf ("Value from main thread:     %d\n", rand ());
  srand (seed);
  thrd_t t;
  assert (thrd_create (&t, rand_invocator_thread, NULL) == thrd_success);
  assert (thrd_join (t, NULL) == thrd_success);

  return 0;
}

^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2023-11-14 18:06 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-10 20:19 rand is not ISO C compliant in Cygwin 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
2023-11-14 18:06                 ` Bruno Haible

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).