X-Recipient: archive-cygwin@delorie.com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BDC2B3858D35
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
	s=default; t=1699652422;
	bh=sL631OII5UB9kGGcgJEnS013mU4LGV1lnspMCzxz+bs=;
	h=Date:Subject:To:References:In-Reply-To:List-Id:List-Unsubscribe:
	 List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:
	 From;
	b=u2rLstBBSadhuhqay9NnFIEtJikN6q6E6+mz7iOpcsZBUojlHHL+w0RgjUAVUy7dv
	 145F2DxyLofH7v89mNNWMjkKLvimYVx6zEe9KRopy3D3/7ynUVN51y601tp+iK3oqu
	 bQD/sbe1wqKUu9WOPw70qEtOjKuCY9QyT9giIddw=
X-Original-To: cygwin@cygwin.com
Delivered-To: cygwin@cygwin.com
DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ECE2D3858D1E
ARC-Filter: OpenARC Filter v1.0.0 sourceware.org ECE2D3858D1E
ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699652386; cv=none;
 b=GsypI+37mufaysE9zt4axPzbum7kX5Nh5XMjX61tzUCYCZtHiYS1IFEqx5xd/bRM067pqAuVkpUAR+DXh5oXlP1xCKJyTSwcq7DDn/EOE9EHRwGdvgYSU67/0h0A59WUsncveINTrt5GT+IjhvSzHB6HgRCnkzD5otmK/QGcT2M=
ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key;
 t=1699652386; c=relaxed/simple;
 bh=FFAVbcdLSerujUhPmvnODtUu1GIeFPYmke4hWN5cvvE=;
 h=Message-ID:Date:MIME-Version:Subject:To:From;
 b=AZqxTXXfPbIeiKhgpqNW6yVZ1k4Gr67qkcVPGM7fGzF6VAS/WFRu2I9yr8/nD3OaIqozE3LO9w68HoVFLacQETeSubPw6uvXR/vlCqo5akOmNtuo4gVvoA9c7y6zZwSaYXElgpBjJ6FFfrLI4X27Whs0OzP7xavoeR8LPEEK8Es=
ARC-Authentication-Results: i=1; server2.sourceware.org
Message-ID: <e099714b-9820-4647-ba40-9db5e471e9aa@huarp.harvard.edu>
Date: Fri, 10 Nov 2023 16:39:42 -0500
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: rand is not ISO C compliant in Cygwin
Content-Language: en-US
To: Bruno Haible <bruno@clisp.org>, cygwin@cygwin.com
References: <9938355.c9vzh5UkMf@nimes>
In-Reply-To: <9938355.c9vzh5UkMf@nimes>
X-Spam-Status: No, score=-2.2 required=5.0 tests=BAYES_00, KAM_DMARC_STATUS,
 SPF_HELO_NONE, SPF_PASS, 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@cygwin.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@cygwin.com>
List-Help: <mailto:cygwin-request@cygwin.com?subject=help>
List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>,
 <mailto:cygwin-request@cygwin.com?subject=subscribe>
From: Norton Allen via Cygwin <cygwin@cygwin.com>
Reply-To: Norton Allen <allen@huarp.harvard.edu>
Content-Type: text/plain; charset="utf-8"; Format="flowed"
Sender: "Cygwin" <cygwin-bounces+archive-cygwin=delorie.com@cygwin.com>
Content-Transfer-Encoding: 8bit
X-MIME-Autoconverted: from base64 to 8bit by delorie.com id 3AALeNp9016060

On 11/10/2023 3:19 PM, Bruno Haible via Cygwin wrote:
> 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.

Since the standard is trying to be precise, this reads to me as though 
Cygwin/(newlib?) has chosen to avoid race conditions by making 
pseudo-random sequences in different threads independent. Although the 
standard does not require this, it does not prohibit it either.


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

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

