samedi 18 novembre 2023

Are function calls that are used to initialize `thread_local` variables guaranteed to be thread-safe?

Consider the following implementation of rand_double(), in which there is a thread_local function variable seed that is initialized using std::random_device{}() in each thread:

auto rand_double(double min, double max) {
    thread_local uint64_t seed = std::random_device{}();
    // ^^ Are the calls to std::random_device guaranteed to not interleave?

    /* Generate random `double` and return it... */
}

I have learned that the initialization of static function variables is guaranteed to be thread-safe. However, I haven't found a source that states if, when using a function to initialize the static function variable, calls to that function will not interleave. Can someone confirm if using a function to initialize static (specifically thread_local - does it make a difference?) function variables is thread-safe?




Aucun commentaire:

Enregistrer un commentaire