windows MSVC 的时间函数是线程安全的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2278919/
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
Are the time functions of MSVC thread-safe?
提问by Jay
In case of Linux, for time functions we have a _r versions Ex: localtime has localtime_r, but in Windows I am unable to find some such functions. Are the Windows time functions inherently thread-safe?
在 Linux 的情况下,对于时间函数,我们有一个 _r 版本例如:localtime 有 localtime_r,但在 Windows 中我找不到一些这样的函数。Windows 时间函数本质上是线程安全的吗?
回答by Chris Becke
With Microsoft Visual Studio you have a choice of c-runtimes to use: typically they were:
使用 Microsoft Visual Studio,您可以选择要使用的 c 运行时:通常它们是:
- static single threaded library (libc)
- Static multithreaded library (libcmt)
- dynamic multithreaded library (msvcrt)
- 静态单线程库 (libc)
- 静态多线程库 (libcmt)
- 动态多线程库 (msvcrt)
The multithreaded libraries are thread safe. The single threaded library was last seen in MSVC 2005 and has been dropped from MSVC 2008.
多线程库是线程安全的。单线程库最后一次出现在 MSVC 2005 中,并已从 MSVC 2008 中删除。
The dll runtime (msvcrt.dll) just has to be thread safe - As the implementation is in a dll and therefore shared between multiple other modules in the process, all of which could be using worker threads, It has to be threadsafe as there would be no sane way to design an application to use it otherwise.
dll 运行时 (msvcrt.dll) 必须是线程安全的——因为实现是在一个 dll 中,因此在进程中的多个其他模块之间共享,所有这些模块都可能使用工作线程,它必须是线程安全的设计应用程序以其他方式使用它是没有理智的方法。
回答by janneb
On windows the non-_r functions are thread safe because they use thread-local storage for the buffer. See e.g. http://msdn.microsoft.com/en-us/library/bf12f0hc(VS.80).aspx
在 Windows 上,非 _r 函数是线程安全的,因为它们使用线程本地存储作为缓冲区。参见例如http://msdn.microsoft.com/en-us/library/bf12f0hc(VS.80).aspx
回答by matekm
I think that Windows localtime_s is thread safe: http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx- at least when using MS CRT
我认为 Windows localtime_s 是线程安全的:http: //msdn.microsoft.com/en-us/library/a442x3ye( VS.80) .aspx- 至少在使用 MS CRT 时
回答by HyLian
I think that they aren't ThreadSafe and there is no _r version. But correct me if I'm wrong.
我认为它们不是 ThreadSafe 并且没有 _r 版本。但如果我错了,请纠正我。
Maybe it's better if you use the own Windows functions, like
如果您使用自己的 Windows 功能,也许会更好,例如