时间函数-线程安全

http://lists.gnu.org/archive/html/bug-commoncpp/2004-05/msg00006.html

本意是找一个线程安全并可重入的 求线程运行时间的函数。

time(), gettimeofday() and possibly localtime() are not thread safe functions. time() is, on some platforms at least, implemented by using gettimeofday().

可重入函数 必然是 线程安全函数 和 异步信号安全函数;

线程安全函数不一定是可重入函数。(http://blog.csdn.net/jiange_zh/article/details/52071428)

The localtime() function need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe.

#include <stdio.h>
#include <time.h>

int main()
{
    time_t time_seconds = time(0);
    struct tm start;
    localtime_r(&time_seconds, &start);
sleep(3);
    time_t time_seconds2 = time(0);
    struct tm end;
    localtime_r(&time_seconds2, &end);
   //printf("%d-%d-%d %d:%d:%d
", now_time.tm_year + 1900, now_time.tm_mon + 1,now_time.tm_mday, now_time.tm_hour, now_time.tm_min, now_time.tm_sec);
    
   //printf("%d-%d-%d %d:%d:%d
", now_time2.tm_year + 1900, now_time2.tm_mon + 1,now_time2.tm_mday, now_time2.tm_hour, now_time2.tm_min, now_time2.tm_sec);

    time_t t = mktime(&end) - mktime(&start);
    printf("t = %d
",t);

}

原文地址:https://www.cnblogs.com/csun/p/6246055.html