From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mo4-p00-ob.smtp.rzone.de (mo4-p00-ob.smtp.rzone.de [85.215.255.23]) by sourceware.org (Postfix) with ESMTPS id 237733858D1E for ; Fri, 10 Nov 2023 20:19:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 237733858D1E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=clisp.org Authentication-Results: sourceware.org; spf=none smtp.mailfrom=clisp.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 237733858D1E Authentication-Results: server2.sourceware.org; arc=pass smtp.remote-ip=85.215.255.23 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1699647591; s=strato-dkim-0002; d=clisp.org; h=Message-ID:Date:Subject:To:From:Cc:Date:From:Subject:Sender; bh=Hsjg060MPj8dfbhKkM//IlXIgfg9i2wflrH1wGjP8do=; b=hV3kg6fKP8Amtzyu+qes6M+0GG9Vpv+O/l2Xwlnq6vTz5jrdgDMBFUmaY3IT5bOUTw 9/MCoZEJaUA6jaYJ7fjWqiuswaRC2+KOSa4bS77xW/RTeIpGDmRPbi6CQUmEUNOGsrt8 uc09wK7lmu7o2szEJ0O0r5UaUXCLvzliJqZgD3O6bmg7ecd7/8A3Yy+dukscYXZgcWwf PoCnR0/zE18Wzm4d2xI26yRy/++L2fsRUogRRfkx/Hix4RLkBfWSOtqDPN+scNQAFNbL /o+fCYiXDUths6UDl4Vupqagw0boiG7mZ2SGIopbgQmLhWiY9W4Ikh8J5g83nCA9dXmK HYqQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1699647591; s=strato-dkim-0003; d=clisp.org; h=Message-ID:Date:Subject:To:From:Cc:Date:From:Subject:Sender; bh=Hsjg060MPj8dfbhKkM//IlXIgfg9i2wflrH1wGjP8do=; b=IcA0qdIh1Q6Wy+684ofK/N+D0lQzd9Efn80GhD0ScthQq//fMIXXl5M79k9oiZzXsT j3HkSQihlanHM3TExfAw== X-RZG-AUTH: ":Ln4Re0+Ic/6oZXR1YgKryK8brlshOcZlIWs+iCP5vnk6shH0WWb0LN8XZoH94zq68+3cfpOVivRrqAkwB7g/1OdqjfT+fMz02A==" Received: from nimes.localnet by smtp.strato.de (RZmta 49.9.1 AUTH) with ESMTPSA id ecf079zAAKJpLGq (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Fri, 10 Nov 2023 21:19:51 +0100 (CET) From: Bruno Haible To: cygwin@cygwin.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 Content-Type: multipart/mixed; boundary="nextPart6050056.MlmRxi5KL8" Content-Transfer-Encoding: 7Bit 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 List-Id: 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 #include #include #include 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 #include #include #include 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--