Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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



It'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.


std::randint is supposed to use thread-local storage for its state. At least according to cppreference: http://en.cppreference.com/w/cpp/experimental/randint


It'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..."




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: