时间函数-线程安全

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\n", 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\n", 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\n",t);

}

 

 

 

转载于:https://www.cnblogs.com/csun/p/6246055.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值