3 ms·
Someone converted our codebase into a multi threaded version, didn't realize that rand() is not thread safe as multiple threads were generating same random numb
by tonmoy 9y ago
Someone converted our codebase into a multi threaded version, didn't realize that rand() is not thread safe as multiple threads were generating same random number from time to time.
PS: that someone was me
- slavik81 9y agoIt's an easy mistake to make. rand() is built around hidden, global mutable state. It's fundamentally a bad design, and it's the same reason I dislike the std::randint proposal.
- xyz6633 9y agostd::randint is supposed to use thread-local storage for its state. At least according to cppreference: http://en.cppreference.com/w/cpp/experimental/randint http://en.cppreference.com/w/cpp/experimental/randint
- slavik81 9y agoIt's not the thread-safety itself that's the issue. The thing is that when you can see the state, it's obvious what can influence what. When you hide the state, there's more documentation to read and interactions are less obvious. "Show me your tables..."