简体   繁体   English

Linux上的标准C mktime线程安全吗?

[英]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 : mktime的手册页没有提到mktime的线程安全性,但它确实提到了这看起来像线程不安全

Calling mktime() also sets the external variable tzname with information about the current time zone. 调用mktime()还会将外部变量tzname设置为有关当前时区的信息。

I know on Linux mktime calls tzset to set tzname, which is a char*[]: 我知道在Linux上mktime调用tzset来设置tzname,这是一个char * []:

extern char *tzname[2]; extern char * tzname [2];

and tzset will read environment variable TZ and file /etc/localtime. 和tzset将读取环境变量TZ和文件/ etc / localtime。 So unless mktime uses a mutex to protect all these operations, I can't see how it can be thread safe. 因此,除非mktime使用互斥锁来保护所有这些操作,否则我无法看到它是如何保证线程安全的。

It's true that mktime has a side effect, but the side effect should be harmless in most programs. 确实, mktime有副作用,但在大多数程序中副作用应该是无害的。

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. 根据POSIX ,副作用就像调用了tzset一样 ,这反过来只是将时区信息从TZ环境变量tzname到C字符串的C字符串数组。 If your application doesn't change TZ , you will have no problem with calling mktime() concurrently. 如果您的应用程序没有更改TZ ,则同时调用mktime()没有问题。

In addition to this, GNU libc's tzset does use a mutex to protect the integrity of tzname . 除此之外,GNU libc的tzset 确实使用互斥锁来保护tzname的完整性。 This is a quality-of-implementation addition not guaranteed by the standard. 这是标准不保证的实施质量补充。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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