I have found online evidence that suggest that srandom is not thread safe. I am unable to proove this because I don't know if this was fixed in recent years. Do you know whether this thread safety issue still exists? If this is true, what would be a work around for srandom.
A simple workaround would be to simply not use srand()/rand() and use some other random number generator library. I believe that implementation of the Mersenne Twister are a popular choice, and may be easier/better to use than most implementations of the standard random number generator.
Why would thread safety matter in a function you only call once?
Originally Posted by chrismiceli
Josuttis claims in "The C++ Standard Library: A Tutorial and Reference" (section 9.8.4) that:
Old global C functions such as rand() store their local state in a static variable. However, this has some disadvantages: For example, the random number generator is inherently thread unsafe, and you can't have two independent streams of random numbers.
This page says that srandom() is not thread-safe: http://www.ncsa.uiuc.edu/UserInfo/Re..._quick_ref.htm
This page suggests that srandom_r() is an equivalent thread-safe function: http://www.llnl.gov/asci/platforms/white/code_dev/
I know that Cygwin has certain _r functions, perhaps your compiler does too.
But why should srandom() be thread-safe? It initializes the random number generator. Surely you can figure out a way to do that in one thread only.
 Three posts while I was typing! Wow . . . [/edit]
I am not using it, this library is.
I can modify the library, but the solution must be extremely portable.
srandom() is "extremely portable"? ;)
Since you usually only call srandom() once, you should be able to work around its thread-unsafeness, or you could probably make a thread-safe wrapper for it if you felt like it.
I can't open that archive on this computer (nor can I install any archive programs here!), so I can't look at the source. Can you post the code that uses srandom()? Are you calling srandom() more than once? (Note: I'm just saying "you" out of habit.)
Prelude's Mersenne Twister implementation found on eternallyconfuzzled.com should be pretty portable. I have a C++ port of that, and there are also Boost's random number generators.
Oh wait, the uploaded implementation file of my C++ version of Prelude's implementation has a bug with the seed generation function.