Just as in title. Is suspect it is, but I couldn't find it anywhere explicitly stated. And for this property I wouldn't like to rely on speculations.
The function rand() is not reentrant or thread-safe, since it uses hidden state that is modified on each call.
Random are thread-safe. However, the concurrent use of the same java. util. Random instance across threads is synchronized and as we have found out tends to trigger contention issues affecting performance of the application.
the standard C printf() and scanf() functions use stdio so they are thread-safe.
The rand() function shall return the next pseudo-random number in the sequence. The rand_r() function shall return a pseudo-random integer. The srand() function shall not return a value.
If you use the multithreaded version of the CRT, all functions are thread safe, because any thread-specific information is stored in TLS. rand_s actually doesn't use state information in the first place, since it just calls an OS API, so question of thread-safety doesn't arise for rand_s. rand(), however depends on a seed value to generate a random number.
Chris said: rand()
is not thread-safe because its internal state is static, but rand_s()
should be thread-safe, however.
Jeff added however that with the multithreaded version of MSVCRT, rand()
's state is held in thread-local storage, so it's okay still.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With