On Tue, Sep 18, 2012 at 09:45:59PM -0400, Ted Unangst wrote: > On Wed, Sep 19, 2012 at 00:48, Alexey Suslikov wrote: > > >> > No, according to posix it should be thread safe. I don't know why, > >> > since rand() is one of the exempted functions, but random() is not. > >> > Standards gods are capricious gods. > >> > >> I think you should stress *should* here. Looking at the > >> implementation, I cannot believe it's actually thread-safe. > >> > >> > > How about rand, srand, srand48 etc? > > Well, like I said :), rand is exempt. There's rand_r for that.
For the rand48 family, the ones getting the seed as an arg are thread-safe. The others are not, and are not required to be safe. Strangely srand48() is not explicitly exempt according to the standard. The implementation is not safe since it modifies global data and you can end up with a mixed state if two calls happen simultaneously. arc4random() is also thread-safe (it has interal locking) and very desirable for other reasons. But no way to save state. -Otto