Random Generate & Thread Safety

Torbjorn Granlund tg at gmplib.org
Thu Nov 17 21:11:36 CET 2011


"james at jamesbb.co.uk" <james at jamesbb.co.uk> writes:

  I understand that the random integer functions are not thread-safe.

That is a misunderstanding.

Please see http://gmplib.org/manual/Reentrancy.html.

  To achieve thread-safety, is it sufficient to keep the gmp_randstate_t structure
  in thread-local storage?

No.  Just like for *any* variable in GMP or elsewhere, gmp_randstate_t
cannot safely be written by two threads, or read by one thread and
written by another, without exclusion mechanisms.  There is nothing
magic about gmp_randstate_t that would make it different.

-- 
Torbjörn


More information about the gmp-discuss mailing list