Commit 5eda59e6 authored by Jean-Baptiste Mouret's avatar Jean-Baptiste Mouret
Browse files

add mutex "to be safe" (there will likely be a small performance hit, but this should be OK for us)

It is still better to have an instance of RandomGenerator in each thread.
parent f3fa9ab7
......@@ -44,6 +44,7 @@
#include <stdlib.h>
#include <random>
#include <utility>
#include <mutex>
namespace limbo {
namespace tools {
......@@ -56,10 +57,15 @@ namespace limbo {
using result_type = typename D::result_type;
RandomGenerator(result_type min, result_type max) :
_dist(min, max), _rgen(std::random_device()()) {}
result_type rand() { return _dist(_rgen); }
result_type rand()
{
std::lock_guard<std::mutex> lock(_mutex);
return _dist(_rgen);
}
private:
D _dist;
std::mt19937 _rgen;
std::mutex _mutex;
};
using rdist_double_t = std::uniform_real_distribution<double>;
using rdist_int_t = std::uniform_int_distribution<int>;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment