Boost logo

Boost Users :

Subject: Re: [Boost-users] thread-safe boost::random_device / boost:mt19937
From: Brian Budge (brian.budge_at_[hidden])
Date: 2012-12-24 12:20:06


On Thu, Dec 20, 2012 at 8:26 AM, Philipp Kraus
<philipp.kraus_at_[hidden]> wrote:
> Hello,
>
> I'm using boost::random_device & boost::mt19937 for creating random numbers.
> I would like to use both within different threads, are both object thread-safe,
> so I can use it without any thread locking?
>
> Thanks
>
> Phil

I doubt they are thread safe due to the performance overhead involved
in making them thread safe. Having written and used other MT-based
RNGs, it could probably double the cost of generating random numbers,
even in cases where thread safety is not required.

If possible, can you construct an object per thread? This is
typically the way that I have used these wrappers (and actually the
reason for writing them). If you need a specific sequence, it might
make sense to generate many random numbers up-front, and then use them
in the thread. If all else fails, you can use your own boost::mutex
and boost::lock_guard to ensure thread safety.

  Brian


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net