[fpc-devel] Random thread-safe
Mark Morgan Lloyd
markMLl.fpc-devel at telemetry.co.uk
Thu Jan 28 18:19:31 CET 2016
Jonas Maebe wrote:
>>>> Also: the entire state of the random number generator consists of
>>>> regular global variables, so it is not safe to use it from multiple
>>>> threads in parallel.
>>
>> Would it make sense to you to make random thread-safe? E.g. with the
>> use of "threadvar" instead of "var" for the global variables in question?
>
> No. The Mersenne twister has a lot of state (about 2KB). Duplicating
> this for every thread, along with associated slowdown, is not worth it.
> It's better to use a class that encapsulates the state of a random
> number generator and then instantiate this class for every thread.
Could I ask for clarification of this please. Are you saying that
Random() will crash if called simultaneously by multiple threads, or
that it will return suboptimal results?
If, as an example, I have two threads doing network or comms jobs and I
want to introduce jitter in the retries, does that mean that I have to
put Random() into a critical section?
--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk
[Opinions above are the author's, not those of his employers or colleagues]
More information about the fpc-devel
mailing list