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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

多线程中使用mktime和setenv函数

發布時間:2023/11/27 生活经验 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 多线程中使用mktime和setenv函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在編寫ATS插件的過程中,發現使用mktime會偶爾出現段錯誤, 經過網上調研,發現mktime等函數不是線程安全的, 于是編寫下面的代碼進行測試.

注意加鎖和不加鎖區別很大, 在mktime中使用多線程, 加上互斥鎖就沒有問題.

//gcc -g mktime_multithread.c -o mktime_multithread -lpthread -std=c99
//
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>pthread_mutex_t mutex;void* test_setenv(void *arg)
{char ac_name[1024] = {};for (int i = 0; i < 10000; i++){snprintf(ac_name, sizeof(ac_name), "stra_1_fe_filter_dt_other_hadoop_arg_%d", i);printf("setenv %s\n",ac_name);pthread_mutex_lock(&mutex);setenv(ac_name," 1500",0);pthread_mutex_unlock(&mutex);}return NULL;
}void* test_mktime(void* arg){for (int i = 0; i < 10000; i++){/*測試非法日期會不會導致程序異常*/struct tm st_tm;memset(&st_tm,0,sizeof(st_tm));strptime("201506270828001","%Y%m%d%H%M",&st_tm);pthread_mutex_lock(&mutex);time_t st_time = mktime(&st_tm);pthread_mutex_unlock(&mutex);printf("time_t=%lu\n",st_time);}return NULL;
}int main(int argc, char* argv[])
{pthread_mutex_init(&mutex,NULL);pthread_t pt[2] = {0};pthread_create(&pt[0], NULL, test_setenv, NULL);pthread_create(&pt[1], NULL, test_mktime, NULL);pthread_join(pt[0], NULL);pthread_join(pt[1], NULL);pthread_mutex_destroy(&mutex);return 0;
}

參考文獻

[1].http://www.xuebuyuan.com/1824402.html

總結

以上是生活随笔為你收集整理的多线程中使用mktime和setenv函数的全部內容,希望文章能夠幫你解決所遇到的問題。

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