Re: localtime deprecated?
Jake Montgomery wrote:
[...]
Could you explain why localtime is considered to be inherently
unsafe for multithreading?
Read the specification. I suppose it could be made thread safe
by using some sort of thread local storage, but in general, the
wording of the standard encourages the use of static data.
It would seem that its thread safety would be implementation
dependent ... especially since AFAIK the C++ standard does not
address threading at all.
The fact that the standard doesn't address threading at all
means that you have undefined behavior as soon as there is more
than one thread.
It would seem that the tm structure returned would be
"per-thread" in any self respecting mutlithreaded standard C++
library.
Well, it's definitely not the case in Posix, which introduced
localtime_r to avoid the problem. It might be the case in some
Windows libraries, however; I'm not sure.
Is that a naive assumption, or am I missing something else in
the definition that makes it inherently unsafe?
I think it's a naive assumption, although I can see where it
makes sense. In practice, early threading systems, at least on
Unix systems, didn't have any direct support for thread local
storage, and emulating it was felt to be too expensive, and
possibly to introduce memory leaks -- you allocate the thread
local memory the first time the function is called within a
given thread, but when do you free it?
--
James Kanze kanze.james@neuf.fr
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]