日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

localtime与localtime_r

發布時間:2024/2/28 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 localtime与localtime_r 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在寫代碼的時候,經常會用到讀取系統時間的函數。很多人都會調用localtime函數來將時間轉換本地時間,但是大家往往會忽略了一點,localtime函數不是線程安全的。如果在多線程里調用localtime函數,很可能會出現問題。

struct tm *localtime(const time_t *clock);

這個函數在返回的時候,返回的是一個指針,實際的內存是localtime內部通過static申請的靜態內存,所以通過localtime調用后的返回值不及時使用的話,很有可能被其他線程localtime調用所覆蓋掉

??? 多線程應用里面,應該用localtime_r函數替代localtime函數,因為localtime_r是線程安全的。

struct tm* localtime_r( const time_t* timer, struct tm* result );


轉載二:

上程序:

[c-sharp]?view plaincopy
  • #include?<cstdlib>??
  • #include?<iostream>??
  • #include?<time.h>??
  • #include?<stdio.h>??
  • ??
  • using?namespace?std;??
  • ??
  • int?main(int?argc,?char?*argv[])??
  • {??
  • ????time_t?tNow?=time(NULL);??
  • ????time_t?tEnd?=?tNow?+?1800;??
  • ????//注意下面兩行的區別??
  • ????struct?tm*?ptm?=?localtime(&tNow);??
  • ????struct?tm*?ptmEnd?=?localtime(&tEnd);??
  • ??
  • ????char?szTmp[50]?=?{0};??
  • ????strftime(szTmp,50,"%H:%M:%S",ptm);??
  • ????char?szEnd[50]?=?{0};??
  • ????strftime(szEnd,50,"%H:%M:%S",ptmEnd);??
  • ??????
  • ??
  • ????printf("%s?/n",szTmp);??
  • ????printf("%s?/n",szEnd);??
  • ??????
  • ??
  • ????system("PAUSE");??
  • ????return?EXIT_SUCCESS;??
  • }??
  • 最后出來的結果是:

    21:18:39

    21:18:39

    和最初想法不一致。

    ?

    查閱localtime的文檔,發現這段話:

    This structure is statically allocated and shared by the functions gmtime and?localtime. Each time either one of these functions is called the content of this structure is overwritten.

    也就是說每次只能同時使用localtime()函數一次,要不就會被重寫!

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

    因此localtime()不是可重入的。同時libc里提供了一個可重入版的函數localtime_r();

    Unlike?localtime(), the reentrant version is not required to set?tzname。

    修改程序:

    [c-sharp]?view plaincopy
  • #include?<cstdlib>??
  • #include?<iostream>??
  • #include?<time.h>??
  • #include?<stdio.h>??
  • ??
  • using?namespace?std;??
  • ??
  • int?main(int?argc,?char?*argv[])??
  • {??
  • ????time_t?tNow?=time(NULL);??
  • ????time_t?tEnd?=?tNow?+?1800;??
  • ??
  • ????//在這里修改程序??
  • ????//struct?tm*?ptm?=?localtime(&tNow);??
  • ????//struct?tm*?ptmEnd?=?localtime(&tEnd);??
  • ????struct?tm?ptm?=?{?0?};??
  • ????struct?tm?ptmEnd?=?{?0?};??
  • ????localtime_r(&tNow,?&ptm);??
  • ????localtime_r(&tEnd,?&ptmEnd);??
  • ??????
  • ????char?szTmp[50]?=?{0};??
  • ????strftime(szTmp,50,"%H:%M:%S",&ptm);??
  • ????char?szEnd[50]?=?{0};??
  • ????strftime(szEnd,50,"%H:%M:%S",&ptmEnd);??
  • ????printf("%s?/n",szTmp);??
  • ????printf("%s?/n",szEnd);??
  • ??????
  • ??
  • ????system("PAUSE");??
  • ????return?EXIT_SUCCESS;??
  • }??
  • 最后出來的結果是:

    10:29:06?
    10:59:06


    超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

    總結

    以上是生活随笔為你收集整理的localtime与localtime_r的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。