Re: localtime() thread-safe or not?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



MarkMark wrote:
> localtime() C Runtime API is documented
> (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_localtime.asp)
> to return result in static memory, which, of course is
> not thread safe.
>
> However, indirect evidence
> (http://support.microsoft.com/default.aspx?scid=kb;en-us;104641)
> appears to suggest that localtime() actually uses Thread
> Local Store which makes it thread safe.
>
> So my question is -- is localtime() really thread safe or
> not?

It depends on your definition of thread safety. If by thread 
safety you mean thread awareness, then yes, localtime(), and 
other CRT fuinctions are thread aware. It promises to work 
correctly being called from different threads (provided CRT 
thread info is properly initialized).

However, if by thread safety you mean data access 
sinchronization, too, then CRT functions will not guarantee 
you that. It's just too expensive and most of the time 
simply unnecessary to embed synchronization objects on all 
CRT functions. If strcpy or memset will be called from 
different threads simultaneously on the same buffer, then 
data corruption is likely. In that sense, CRT functions are 
not thread safe. 


.



Relevant Pages

  • Re: localtime() thread-safe or not?

    (microsoft.public.vc.language)
  • Re: localtime() thread-safe or not?
    ... to return result in static memory, which, of course is not thread safe. ... indirect evidence appears to suggest that localtimeactually uses Thread Local Store which makes it thread safe. ... So my question is -- is localtime() really thread safe or not? ...
    (microsoft.public.vc.language)
  • localtime in Solaris 9 is not thread safe?
    ... I'm using pthreadprogramming in Solaris 9 and find that localtime is ... not thread safe. ... Franklin ...
    (comp.unix.solaris)