简体   繁体   中英

Is standard C mktime thread safe on linux?

The man page of mktime didn't mention thread safety of mktime, but it did mention this which make it look like thread unsafe :

Calling mktime() also sets the external variable tzname with information about the current time zone.

I know on Linux mktime calls tzset to set tzname, which is a char*[]:

extern char *tzname[2];

and tzset will read environment variable TZ and file /etc/localtime. So unless mktime uses a mutex to protect all these operations, I can't see how it can be thread safe.

It's true that mktime has a side effect, but the side effect should be harmless in most programs.

According to POSIX , the side effect will be as if tzset has been called, which in turn merely copies the timezone information from the TZ environment variable to the tzname array of C strings. If your application doesn't change TZ , you will have no problem with calling mktime() concurrently.

In addition to this, GNU libc's tzset does use a mutex to protect the integrity of tzname . This is a quality-of-implementation addition not guaranteed by the standard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM