dimanche 6 décembre 2015

C++ Are pseudo random number generators thread safe?

Q1:Are pseudo random number generators thread safe ?Can I use a shared generator in multiple threads ?

#include <random>
#include <math.h>  
using namespace std;//Should I use thread_local here?
random_device seed; //Should I use thread_local here?
default_random_engine engine(seed());
int random_int(int x, int y)
{
    binomial_distribution<int> distribution(y - x);
    return distribution(engine) + x;
}

Q2:Does thread safe means lock-free ? If a class is thread safe ,then does that mean I can use a shared instance of it in multiple threads without locking it ?




Aucun commentaire:

Enregistrer un commentaire