In case of Linux, for time functions we have a _r versions Ex: local开发者_开发知识库time has localtime_r, but in Windows I am unable to find some such functions. Are the Windows time functions inherently thread-safe?
With Microsoft Visual Studio you have a choice of c-runtimes to use: typically they were:
- static single threaded library (libc)
- Static multithreaded library (libcmt)
- dynamic multithreaded library (msvcrt)
The multithreaded libraries are thread safe. The single threaded library was last seen in MSVC 2005 and has been dropped from 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.
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
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
I think that they aren't ThreadSafe and there is no _r version. But correct me if I'm wrong.
Maybe it's better if you use the own Windows functions, like
GetSystemTime, GetSystemTimeAsFileTime or GetLocalTime
精彩评论