X-Recipient: archive-cygwin AT delorie DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3FC133858C01 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; s=default; t=1699896901; bh=+gJ2CTLBm+chj0QzDoNKFs8i34VEa+e0bFmXgwp42Kk=; h=Date:Subject:To:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=r1maVoOjTv5U0kMSpEEna/spfBGLccYIXui/9uO75QeXHCwrHsMXiXzlUsQioIZ/9 ijV+f0jOW5iN+nDqyKmN5jLSV7+Y/2M1GNO/8ilAuHuxasu1rY1CjilBZ+5r7zSaH8 9RGcdyAefK0AppbMmRuxigHNrAXFJplvQmB0TnVE= X-Original-To: cygwin AT cygwin DOT com Delivered-To: cygwin AT cygwin DOT com DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 271913858D37 ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 271913858D37 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699896886; cv=none; b=JK/utzM2wbQQzR1yWtbeqFXlj0NM+bfZs0PvdvihZI666sJJpcMAh0aq/Z6BltxHOPOW1VjxLYweH4BU58a5gRWrfs6VEh51TRAfTs8WyH6e02zlBZlu9+vlEQLq4nZxY3GoMUeCd/4/7NvzXe5PQnWxdSO7j6EQJHZfXfBSy0c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699896886; c=relaxed/simple; bh=mKI49SKh9MfWqe+UmEpAnSCd72iqTHq5Zd7lbk8qmKc=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=HLtfPXfbK8yB7RuLyY3API1RG43s8tNp2jyzXBE7ZVloEck75jNjgTsLSM+14BY4lCMFXGRlDETuaW2vPNsRRUaM7uAYGP5YJ7iRsN3TlDZLlrndPLfszIYZwFBN+vEFnYJoGhEMKyMjM9a55mWirhozCXVLeXbuc3FXIPMaZa4= ARC-Authentication-Results: i=1; server2.sourceware.org X-Authority-Analysis: v=2.4 cv=Cousz10D c=1 sm=1 tr=0 ts=65525e34 a=DxHlV3/gbUaP7LOF0QAmaA==:117 a=DxHlV3/gbUaP7LOF0QAmaA==:17 a=IkcTkHD0fZMA:10 a=PxpbvmigVv2tRuUqSXQA:9 a=QEXdDO2ut3YA:10 Message-ID: Date: Mon, 13 Nov 2023 10:34:43 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: random is not multithread-safe in Cygwin Content-Language: en-CA To: cygwin AT cygwin DOT com References: <3811044 DOT 57xzQst1vy AT nimes> Organization: Inglis In-Reply-To: X-CMAE-Envelope: MS4xfK0twq5HwM0xPtYVo/BkA+lWi6dJLQ/fkzT4EFGoNB4BcJ5+wN2ZYA9O1XN2o2s2gP0MPioRvD1ABVgGQ7A4YZPvuJLeBbfx8qEuUJNc4/QE5+AbG60p Uy7iVfb05fEorNR5GE6+efSlbOKRANLHy+7bqETSX8WXrtLRNLI7WFAfGH/0N0LC17HlplKEHxELD3l0l99j0kRAoKziUyD7ioI= X-Spam-Status: No, score=-3.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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 AT cygwin DOT com X-Mailman-Version: 2.1.30 Precedence: list List-Id: General Cygwin discussions and problem reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Brian Inglis via Cygwin Reply-To: cygwin AT cygwin DOT com Cc: Brian Inglis , Bruno Haible Content-Type: text/plain; charset="utf-8"; Format="flowed" Errors-To: cygwin-bounces+archive-cygwin=delorie DOT com AT cygwin DOT com Sender: "Cygwin" Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from base64 to 8bit by delorie.com id 3ADHZ2jV030825 On 2023-11-13 09:12, Corinna Vinschen via Cygwin wrote: > On Nov 10 17:19, Bruno Haible via Cygwin wrote: >> The function 'random' is, unlike 'rand', not marked as not MT-safe in POSIX >> [1][2]. Thus it must be multithread-safe [3]: >> "Each function defined in the System Interfaces volume of POSIX.1-2017 >> is thread-safe unless explicitly stated otherwise." >> And indeed glibc, musl libc, AIX, Android, and even NetBSD implement it in a >> multithread-safe way. > Our code is from FreeBSD, originally. I checked the latest code from > FreeBSD. It doesn't lock anything in random() and generates the same > error when running the same test app. > Why is that ok for FreeBSD? It appears that random(3) is intended to provide a single random series during execution of a program with simple needs; behaviour is not reproducible when threaded, says POSIX, newlib, and Linux (below). From POSIX The Open Group Base Specifications Issue 7, 2018 edition IEEE Std 1003.1-2017 Volume 2: System Interfaces and initstate(3p) to which random(3p) refers and defers but does not link or .so: "NAME random — generate pseudo‐random number SYNOPSIS #include long random(void); DESCRIPTION Refer to initstate()." * That's all folks! "NAME initstate, random, setstate, srandom - pseudo-random number functions ... APPLICATION USAGE ... Threaded applications should use erand48(), nrand48(), or jrand48() instead of random() when an independent random number sequence in multiple threads is required." From newlib: "NAME random, srandom - pseudo-random numbers ... NOTES random and srandom are unsafe for multi-threaded applications. _XOPEN_SOURCE may be any value >= 500. PORTABILITY random is required by XSI. This implementation uses the same algorithm as rand." * Newlib, and presumably FreeBSD, do not support initstate, setstate, or > 8 (for amd64/x86_64) bytes state. From man-pages-linux random(3): "CAVEATS ... The random() function should not be used in multithreaded programs where reproducible behavior is required. Use random_r(3) for that purpose." * As usual, random_r(3) requires the caller to provide an initialized state vector buffer (of 8-256 bytes), allowing the caller to decide the amount of randomness and scope of the random series, and possibly call initstate_r(3), to control the seed. -- Take care. Thanks, Brian Inglis Calgary, Alberta, Canada La perfection est atteinte Perfection is achieved non pas lorsqu'il n'y a plus rien à ajouter not when there is no more to add mais lorsqu'il n'y a plus rien à retirer but when there is no more to cut -- Antoine de Saint-Exupéry -- 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