Windows 的 rand_s 线程安全吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/143108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is Windows' rand_s thread-safe?
提问by Pawe? Hajdan
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.
就像标题一样。怀疑它是,但我找不到任何明确说明的地方。对于这个财产,我不想依赖猜测。
采纳答案by user15071
If you use the multithreadedversion 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.
如果您使用CRT的多线程版本,则所有函数都是线程安全的,因为任何特定于线程的信息都存储在TLS 中。rand_s 实际上首先不使用状态信息,因为它只是调用 OS API,因此 rand_s 不会出现线程安全问题。rand(),但是依赖于种子值来生成随机数。
回答by mmcdole
Chris said: rand()
is not thread-safe because its internal state is static, but rand_s()
should be thread-safe, however.
Chris 说:rand()
不是线程安全的,因为它的内部状态是静态的,但是rand_s()
应该是线程安全的。
Jeff added however that with the multithreaded version of MSVCRT, rand()
's state is held in thread-local storage, so it's okay still.
然而,Jeff 补充说,对于 MSVCRT 的多线程版本,rand()
的状态保存在线程本地存储中,所以它仍然可以。
回答by Michael Burr
Visual Studio comes with the source to the runtime library. While some of it can be rather painful to wade through, rand_s() is pretty simple.
Visual Studio 附带运行时库的源代码。虽然其中一些过程可能会很痛苦,但 rand_s() 非常简单。
All rand_s() does is call SystemFunction036() in ADVAPI32.DLL to get the random value. Anything in ADVAPI32.DLL should be thread-safe.
rand_s() 所做的只是调用 ADVAPI32.DLL 中的 SystemFunction036() 来获取随机值。ADVAPI32.DLL 中的任何内容都应该是线程安全的。
For its part, rand_s() gets the pointer to that function in a thread-safe manner.
就其本身而言, rand_s() 以线程安全的方式获取指向该函数的指针。
回答by Michael Burr
I don't know if rand_s is thread-safe, but it seems like it probably is, since it seems to make a round-trip to the OS for entropy. (as long as you link to the VC++ multi-thread CRT, all bets are off if you link to the single-thread one)
我不知道 rand_s 是否是线程安全的,但它似乎是线程安全的,因为它似乎是为了熵而往返于操作系统。(只要您链接到 VC++ 多线程 CRT,如果您链接到单线程 CRT,则所有赌注都将关闭)
If it's supported by windows CRT, you can try a call to rand_r which is the posix reentrant version of rand. OR even better boost::random, if you're already using boost.
如果 Windows CRT 支持它,您可以尝试调用 rand_r,它是 rand 的 posix 可重入版本。或者甚至更好的 boost::random,如果你已经在使用 boost。
considering how pervasive multi-threading will be soon, no one should be using rand() anymore in new code - always try to use rand_r/rand_s/boost/various platform-dependent secure rands/etc.
考虑到多线程将很快普及,没有人应该在新代码中再使用 rand() - 总是尝试使用 rand_r/rand_s/boost/各种依赖于平台的安全 rands/等。
回答by Jeff Hubbard
I can't think of any reason why rand_s() or even rand() wouldn't be thread safe.
我想不出 rand_s() 甚至 rand() 不是线程安全的任何原因。