Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is a `std::mt19937` static function variable thread-safe?

I have a function rand_double() which generates a random floating-point value using a static function variable with type std::mt19937. Several threads may use this function concurrently.

#include <random>

auto random_double(double min, double max) {
    static std::mt19937 generator{std::random_device{}()}; // Forgot to seed it, thanks @user4581301
    return std::uniform_real_distribution<>{min, max}(generator);
}

My first question is, is the above code as-is thread safe? I know that since C++11 the initialization of static function variables is always thread-safe, but how about the concurrent use of generator?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

To generate a random number, the std::uniform_real_distribution calls operator() of the argument generator.

In case of mersenne twister the operator has the side effect:

"The state of the engine is advanced by one position"

which is not (by any indicator) considered a thread-safe operation.

You can use it as thread_local which would have the intended effect i.e. static in each specific thread:

auto random_double(double min, double max) {
    thread_local std::mt19937 generator(std::random_device{}());
    return std::uniform_real_distribution<>{min, max}(generator);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading