時間函數-線程安全


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);

}

 

 

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM