UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2007-11-22 06:18 PM

urw
Member
Registered: 2007-07-16
Posts: 9

Re: linux time functions thread safety

I know that some of the time functions on linux are not thread safe, e.g. gmtime, ctime, since they return a pointer to a static allocated struct.

Does anyone knows if the two following time functions are thread safe ?
                   1)  time_t time ( time_t * timer );
                   2)  int gettimeofday(struct timeval *tv, struct timezone *tz);

Are there any issues I should be aware to when using these functions (I'm working with C++ on linux) ?

Thanks in advance.

Offline

#2 2007-11-22 07:30 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: linux time functions thread safety

Yes, they are thread-safe.

Two things that give it away:

- It are systemcalls, and those are always thread safe (on any sane system).

- They take a pointer to a structure you allocated, so they don't do any memory handling, but only touch what you give them.

Of course most thread-safe functions can be used in a non-thread-safe way.

Offline

#3 2007-11-22 07:39 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: linux time functions thread safety

Also, you'll probably find that there are thread-safe *_r() versions of many of the
non-thread-safe functions, as well...  Eg: ctime_r(), localtime_r(), etc...

Offline

#4 2007-11-24 05:36 PM

urw
Member
Registered: 2007-07-16
Posts: 9

Re: linux time functions thread safety

Offline

#5 2007-11-24 08:17 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: linux time functions thread safety

Offline

#6 2007-11-25 08:19 AM

urw
Member
Registered: 2007-07-16
Posts: 9

Re: linux time functions thread safety

Thanks a lot for your detailed answers,
you helped a lot !

Offline

Board footer

Powered by FluxBB