• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Prayer

            在一般中尋求卓越
            posts - 1256, comments - 190, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            linux線程的私有數據保護

            Posted on 2009-07-05 14:27 Prayer 閱讀(2361) 評論(0)  編輯 收藏 引用 所屬分類: LINUX/UNIX/AIX
            linux下的線程真是很有趣,各種在windows編程里看不到的技巧在這里盡顯無余。在一個進程里有許多的線程,這些線程共享進程里的所有資源。包括數據空間,所以全局變量是為所有的線程所共享的。但如果線程里的全局變量為所有的線程所共享會出現一些問題。比如如果代碼量很大的話那么名字的命名都是一個問題。如果兩個線程有相同的全局erron變量那么線程2可以會用到線程1的出錯提示。

            這個問題可以通過創建線程的私有數據來解決(thread-specific Data,TSD)。一個線程里的TSD只有這個線程可以訪問。

            TSD采用了一種稱之為私有數據的技術,即一個鍵對應多個數據值。意思就好比用一個數據結構,這個結構的結構名就是鍵值,在這個結構里有許多的數據,這些數據封閉在這個結構里。線程可以通過這個結構名即鍵值來訪問其所屬的數據結構。

            創建TSD有三個步驟:創建一個鍵(即創建一個數據結構),為這個鍵設置線程的私有數據(即為這個結構體里的數據賦值)。刪除鍵值。

            三個步驟分別對應的系統函數了:

            int pthread_key_create(pthread_key_t *key, void (*destructor)(void*));

            int pthread_setspecific(pthread_key_t key, const void *value);

            int pthread_key_delete(pthread_key_t key);


            創建了TSD后線程可以用下面的函數來讀取數據。

            void *pthread_getspecific(pthread_key_t key);

            下面代碼演示創建TSD

            1 #include<stdio.h>

            2 #include<string.h>

            3 #include<pthread.h>

            4

            5 pthread_key_t key;

            6 void *thread2(void *arg)

            7 {

            8 int tsd=5;

            9 printf("thread %u is running\n",pthread_self());

            10 pthread_setspecific(key,(void*)tsd);

            11 printf("thread %u returns %d\n",pthread_self(),pthread_getspecific(key));

            12 }

            13 void *thread1(void *arg)

            14 {

            15 int tsd=0;

            16 pthread_t thid2;

            17 printf("thread %u is running \n",pthread_self());

            18 pthread_setspecific(key,(void*)tsd);

            19 pthread_create(&thid2,NULL,thread2,NULL);

            20 sleep(5);

            21 printf("thread %u returns %\n",pthread_self(),pthread_getspecific(key));

            22 }

            23 int main()

            24 {

            25 pthread_t thid1;

            26 printf("main thread begins running \n");

            27 pthread_key_create(&key,NULL);

            28 pthread_create(&thid1,NULL,thread1,NULL);

            29 sleep(3);

            30 pthread_key_delete(key);

            31 printf("main thread exit\n");

            32 return 0;

            33 }

            程序首先包涵所須的頭文件。程序分三個函數,thread2()thread1(),main()。線程2通過線程一的函數來創建。在main()函數里通過調用pthread_key_create()創建了一個TSD鍵值key。然后調用pthread_create()函數創建線程1。線程1開始運行。在線程函數里有要保護的私有數據tsd=0;

            通過調用pthread_key_setspecific()函數把tsd設置到鍵值key當中。接著調用pthread_create()創建線程2。然后沉睡5秒,最后通過調用pthread_key_getspecific(),打印出鍵值。在線程2函數里先定義要保護的私有數據tsd=5;然后調用pthread_key_specific()函數設置tsd5key里。

            在編譯的時候要用到pthread.a庫,形式為:

            ong@ubuntu:~/myjc/myc$ gcc -o tsd tsd.c -lpthread

            99久久精品国内| 色88久久久久高潮综合影院| 久久综合九色综合久99| 欧美激情精品久久久久| 欧美久久亚洲精品| 久久综合鬼色88久久精品综合自在自线噜噜 | 久久九九久精品国产免费直播| 尹人香蕉久久99天天拍| 日产精品久久久久久久性色| 国产高潮国产高潮久久久91 | yy6080久久| 色综合久久久久网| 国产成人精品综合久久久久| 99久久精品九九亚洲精品| 亚洲精品乱码久久久久久蜜桃不卡| 狠狠色丁香婷婷久久综合不卡 | 97久久超碰国产精品2021| 久久无码人妻精品一区二区三区| 色偷偷88888欧美精品久久久 | 精品久久一区二区| 无码人妻久久一区二区三区免费丨| 狠狠色丁香久久婷婷综| 亚洲国产欧美国产综合久久 | 99久久国产主播综合精品| 亚洲精品国产美女久久久| 亚洲欧美国产日韩综合久久| 久久国产一区二区| 少妇内射兰兰久久| 国产精品久久久久久久久久影院 | 99久久精品日本一区二区免费| 久久嫩草影院免费看夜色| 青青草国产精品久久久久| 69SEX久久精品国产麻豆| 久久午夜无码鲁丝片| 亚洲成色www久久网站夜月| 深夜久久AAAAA级毛片免费看| 国产91久久综合| 久久人人超碰精品CAOPOREN| 久久久久国产精品三级网| 精品久久久久久无码中文字幕 | 蜜臀av性久久久久蜜臀aⅴ麻豆|