www.delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2023/11/10/15:20:33

X-Recipient: archive-cygwin AT delorie DOT com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 694C93858422
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
s=default; t=1699647631;
bh=vjvCru1nYfVau1BOc9M+O9gvrVFu5mxiUbHZ0WXjOqA=;
h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post:
List-Help:List-Subscribe:From:Reply-To:From;
b=j5O/GxOztE2GKHB79GIodBymgg9PD7kLxzWQMztVLM2aF86LMrVeRipHK9duVe4NA
jJjZ06PO+dwDXvK7mlI6FJzweI6BH2/O5XAN7FhKtElOVuUsq3RXvmfl/2UkDu7AV5
30vqdjWeyzl4opedJzepnBAX1egrlqTt9xqr5JTg=
X-Original-To: cygwin AT cygwin DOT com
Delivered-To: cygwin AT cygwin DOT com
DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 237733858D1E
ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 237733858D1E
ARC-Seal: i=2; a=rsa-sha256; d=sourceware.org; s=key; t=1699647595; cv=pass;
b=QrvyOTR/ndOkVWYET0Cw3XZJbYjiYIlOve13U2ENLzrxv4sVabhPlg0dUCbG7dHY6VuRlpOgjB91NxybzM/nEHO4h8YusJSioSvBvesjNCDkHjoMk3l0ljSQ9MBMdpRrmGIyDbOkXIae+nLA1Wx7BZcYPA8+LfErAV+SBHdA+C4=
ARC-Message-Signature: i=2; a=rsa-sha256; d=sourceware.org; s=key;
t=1699647595; c=relaxed/simple;
bh=SdOze9Rn7vsDllMQFThF8gQqUgj52Bo8XbwN1fPmyw8=;
h=DKIM-Signature:DKIM-Signature:From:To:Subject:Date:Message-ID:
MIME-Version;
b=Ur4a6qMTjXHgHxj3EQPyzt+MiRWc8Q76QtYOXqYy17MyFsBsBfE/ViASjy/stxZ2QTbw9Q1076LaBeljshkRQFlcDkIVieYWJz3axnPNKY+Lk8Z+H3EQ3xEf9d1NT81qFCmFRTxrJw079st9ZzM1J0ZMukSWn6//VOokP4Ku33A=
ARC-Authentication-Results: i=2; server2.sourceware.org
ARC-Seal: i=1; a=rsa-sha256; t=1699647591; cv=none;
d=strato.com; s=strato-dkim-0002;
b=HF7p1C+JOBTQTX8gbJd7YSIz4cGNzs76P+bU0k31mlP0JDEbA4Y4JQl71OW/OoKlqq
22fndIYt5+yCLc0lTsbichCU7j+dbqv43qILyfWN2NtitF3/NlvenOm5nwFmgD1nLl+4
y5T3COp5sPzMr4cap1JjOS0C0ZVwfLLfT/8omSyA+2W2i9a6INdGCs1pteWkAJj/uFt6
0g7ADyFj1++EFlFkAW4EmsHrEAe3gcWHIBA0gPOhdjg32QNAY8iD93ANW7DI+1/Enk5Q
ANVArNpSZEz7SmM4jedehKTnj7RoTO+qL15ev/8oLYum8k3BdUMjk3KMbWyggs4ZhLNS
ZOGQ==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1699647591;
s=strato-dkim-0002; d=strato.com;
h=Message-ID:Date:Subject:To:From:Cc:Date:From:Subject:Sender;
bh=Hsjg060MPj8dfbhKkM//IlXIgfg9i2wflrH1wGjP8do=;
b=UXBMw3D/0eFs/MMjVsHPW4rRFyf41I60WqyIXsp43jXXG/+6qwwu7HSH15bkPxzV8c
+0mfLqf5gvw1U94/9Som2qf7GaBsMCcOWnmY755qj0e3EMM/o5tGoi92StASITi+bkH0
Nu6kJiLGTZwBAXVvvh5By86k3/MT0XhsZs1YDtzoMtwXJoDjrDyO9HnbjBz3wBB8KXRb
++Fa2SHSA0L2HSiNzzL98WpPV2h9inIY5Pr0JzcD7mI30qbxMsawGgt5NvIESrusCH9D
+WtyZ+VbCD1HGAWekxkJA7Nok3gXb3a+8X9yKYDlCPfhp8COcHkYOx36lELzFCzc7gBQ
qD/Q==
ARC-Authentication-Results: i=1; strato.com;
arc=none;
dkim=none
X-RZG-CLASS-ID: mo00
X-RZG-AUTH: ":Ln4Re0+Ic/6oZXR1YgKryK8brlshOcZlIWs+iCP5vnk6shH0WWb0LN8XZoH94zq68+3cfpOVivRrqAkwB7g/1OdqjfT+fMz02A=="
To: cygwin AT cygwin DOT com
Subject: rand is not ISO C compliant in Cygwin
Date: Fri, 10 Nov 2023 21:19:51 +0100
Message-ID: <9938355.c9vzh5UkMf@nimes>
MIME-Version: 1.0
X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00, DKIM_SIGNED,
DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE,
RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_NONE, TXREP,
T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6
X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on
server2.sourceware.org
X-BeenThere: cygwin AT cygwin DOT com
X-Mailman-Version: 2.1.30
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Archive: <https://cygwin.com/pipermail/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-request AT cygwin DOT com?subject=help>
List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=subscribe>
From: Bruno Haible via Cygwin <cygwin AT cygwin DOT com>
Reply-To: Bruno Haible <bruno AT clisp DOT org>
Sender: "Cygwin" <cygwin-bounces+archive-cygwin=delorie DOT com AT cygwin DOT com>

This is a multi-part message in MIME format.

--nextPart6050056.MlmRxi5KL8
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="iso-8859-1"

ISO C 23 =A7 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


--nextPart6050056.MlmRxi5KL8
Content-Disposition: attachment; filename="rand-in-posix-thread.c"
Content-Transfer-Encoding: 7Bit
Content-Type: text/x-csrc; charset="UTF-8"; name="rand-in-posix-thread.c"

#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;
}

--nextPart6050056.MlmRxi5KL8
Content-Disposition: attachment; filename="rand-in-isoc-thread.c"
Content-Transfer-Encoding: 7Bit
Content-Type: text/x-csrc; charset="UTF-8"; name="rand-in-isoc-thread.c"

#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;
}

--nextPart6050056.MlmRxi5KL8
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

--nextPart6050056.MlmRxi5KL8--



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019