linux執行緒私有資料
阿新 • • 發佈:2019-01-08
今天在看執行緒的私有資料時,一直想找個例子,實際的驗證下,用資料告訴自己:"對,就是那樣的,那就是TSD“,於是乎我看到了這個例子
http://www.ibm.com/developerworks/cn/linux/thread/posix_threadapi/part2/,不得不說IBMdeveloperworkshop這的很好,有很多非常優秀的文章,至於原創與否我沒有考證,不下結論,於是乎下面的程式碼,紛紛的被轉載,轉載,繼續轉載。我百度
”執行緒私有資料“,前幾篇都是來自對IBM的轉載,我暈死了,那個例子個人真心感覺不是很好的體現了執行緒私有資料的特性,另外作者專門說明:以下這個例子沒有什麼實際意義,只是說明如何使用,以及能夠使用這一機制達到儲存 執行緒私有資料的目的。
原作者程式碼:
好吧,吐槽完畢,上自己的程式碼,證明下linux的執行緒私有資料#include <stdio.h> #include <pthread.h> pthread_key_t key; void echomsg(int t) { printf("destructor excuted in thread %d,param=%d\n",pthread_self(),t); } void * child1(void *arg) { int tid=pthread_self(); printf("thread %d enter\n",tid); pthread_setspecific(key,(void *)tid); sleep(2); printf("thread %d returns %d\n",tid,pthread_getspecific(key)); sleep(5); } void * child2(void *arg) { int tid=pthread_self(); printf("thread %d enter\n",tid); pthread_setspecific(key,(void *)tid); sleep(1); printf("thread %d returns %d\n",tid,pthread_getspecific(key)); sleep(5); } int main(void) { int tid1,tid2; printf("hello\n"); pthread_key_create(&key,echomsg); pthread_create(&tid1,NULL,child1,NULL); pthread_create(&tid2,NULL,child2,NULL); sleep(10); pthread_key_delete(key); printf("main thread exit\n"); return 0;
使巨集定義判斷為假,可以作對比測試。[[email protected] pthread_key_create]$ less main.c #include <stdio.h> #include <string.h> #include <pthread.h> #include <unistd.h> #define KEY pthread_key_t key; int tsd=10; void * thread2(void *arg) { printf("before enter thread 2 the global's tsd is %d\n",tsd); #ifdef KEY pthread_setspecific(key,(void *)tsd); key=5; printf("after thread2 modify the thread2's own tsd is ----- %d\n",pthread_getspecific(key)); #endif printf("after modify the global's tsd is %d \n",tsd); pthread_exit((void*)0); return NULL; } void * thread1(void *arg) { printf("before enter the thread 1 the global's tsd is %d\n",tsd); pthread_t thid2; #ifdef KEY pthread_setspecific(key,(void *)tsd); key=0; printf("after thread1 modify ----thread1's own tsd is %d\n",pthread_getspecific(key)); #endif pthread_create(&thid2,NULL,thread2,NULL); pthread_join(thid2,NULL); #ifdef KEY printf("after thread2 modify thread1's own tsd is ----- %d\n",pthread_getspecific(key)); #endif printf("after the thread2 the global's tsd is %d\n",tsd); pthread_exit((void*)0); } void test1() { pthread_t thid1; printf("main thread begins running\n"); #ifdef KEY pthread_key_create(&key,NULL); #endif pthread_create(&thid1,NULL,thread1,NULL); pthread_join(thid1,NULL); sleep(3); #ifdef KEY pthread_key_delete(key); #endif printf("main thread exit\n"); } int main(int argc, char **argv) { test1(); return 0; }
執行結果如下:
[[email protected] pthread_key_create]$ ./run
main thread begins running
before enter the thread 1 the global's tsd is 10
after thread1 modify ----thread1's own tsd is 10
before enter thread 2 the global's tsd is 10
after thread2 modify the thread2's own tsd is ----- 0
after modify the global's tsd is 10
after thread2 modify thread1's own tsd is ----- 0
after the thread2 the global's tsd is 10
main thread exit
[[email protected] pthread_key_create]$
執行緒1把全域性變數設定為自己的私有資料,建立執行緒2,執行緒2獲得資料,資料依舊是10,也就是說執行緒1修改沒有影響全域性,執行緒2設定為自己的私有資料,繼續修改之,回到執行緒1,取出私有資料,還是自己設定的資料,也就是執行緒2的修改沒有影響執行緒1的私有資料,兩個執行緒都退出,全域性變數依舊是10.