Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 2 Dec 2020 23:44:23 -0800
From:      Conrad Meyer <cem@freebsd.org>
To:        Andriy Gapon <avg@freebsd.org>
Cc:        FreeBSD Current <freebsd-current@freebsd.org>
Subject:   Re: rand() is racy in multi-threaded programs?
Message-ID:  <CAG6CVpXekxF27L5dQrdDY=AAnjgPQ=2W9GM4wmhA91J5tjik1g@mail.gmail.com>
In-Reply-To: <22f5dfc1-c709-6911-b8b0-121e4c40affc@FreeBSD.org>
References:  <5b827768-eb46-07f3-5b44-49627779786e@FreeBSD.org> <CAG6CVpXdrivsAk45dDKtk_0M3wuZ-WkdBz1HWHtr2GwRa6v78Q@mail.gmail.com> <22f5dfc1-c709-6911-b8b0-121e4c40affc@FreeBSD.org>

next in thread | previous in thread | raw e-mail | index | archive | help
Hi Andriy,

On Wed, Dec 2, 2020 at 11:34 PM Andriy Gapon <avg@freebsd.org> wrote:
>
> On 03/12/2020 01:20, Conrad Meyer wrote:
> > Rand(3) is explicitly unsafe to use from concurrent threads without som=
e
> > external serialization, even after initialization. I=E2=80=99d suggest =
using a different
> > API.
>
> thank you!
> Just want to check, unsafe in terms of bogus results (with respect to
> randomness) or unsafe as in may crash?

Well, unsafe in that it's a data race, which is formally undefined
behavior in C (if I understand correctly).  So anything could happen,
including a crash.  In practice, you would probably see something more
like the former (bogus results, e.g., multiple calls returning the
same number because the state wasn't updated atomically, or something
like that) rather than a crash.

Best,
Conrad



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAG6CVpXekxF27L5dQrdDY=AAnjgPQ=2W9GM4wmhA91J5tjik1g>