[lug] localtime() returns pointer - free it?

Tkil tkil at scrye.com
Thu Nov 30 10:12:05 MST 2000


>>>>> "Michael" == Michael J Hammel <mjhammel at graphics-muse.org> writes:

Michael> I've never really worried about being threadsafe since I've
Michael> never written threaded code,

well, it's something to consider even for normal code (as you
implicitly indicated, just by asking this question!)  further, it's
important to be aware of global side-effects from any function; in
addition to changing any previously-obtained value (if it hasn't been
copied out of the static extern storage area), `localtime' also
adjusts the global extern "tzname" with time zone information.

Michael> but for argument sake - is there an alternative for
Michael> retrieving local time that *is* thread safe?

doing a quick google search for "localtime thread-safe" returned this
discussion of the POSIX answer to this question: "localtime_r", which
takes a "result" pointer as one of the arguments (similar to how
"time" works).

   http://www.ludd.luth.se/~ragge/book/misc/threadsr.htm

someone wrote a proposal to the C9X working group about adding the
"_r" functions to the upcoming standard:

   http://home.flash.net/~dtribble/text/c9xthr.txt

so it looks like the "_r" versions were designed for exactly this
situation, where you want thread-safe versions of the originals.
(furthermore, the "_r" apparently stands for "reentrant".)  my box at
home doesn't seem to have these functions in the man pages, but i
would presume that glibc 2.2 has them.  anyone want to check?

t.




More information about the LUG mailing list