Closed paulidale closed 5 years ago
~~It might be more prudent to use rand_r
and to seed using gettid() ^ time(NULL)
.
Getting this thread safe could be fun.~~
This has been updated so that the access to rand(3)
is thread safe.
Thanks for the suggestion. Yeah you are correct... by seeding in each shuffle, if they happen in quick succession, there isn't really any randomness going on. A single seed call would be better. The code is under development to update it with the final version of the draft so I will add this in.
C++ will deconstruct the mutex at the end of the function and this unlocks it.
It's worse than reseeding with the same value (which is bad), the non thread-safe calls to rand(3)
will also have problems.
Seeding the pseudo random number generator before every shuffle using a value that changes once per second will mean that the different threads will be using the same sequence of random values. This is not intended.
Instead, seed the pseudo random number generator onces globally and each thread will get a different sequence of values.