• <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 閱讀(2364) 評論(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

            伊人色综合久久天天人手人婷 | 亚洲AV成人无码久久精品老人| 久久久久99精品成人片| 色欲综合久久躁天天躁| 亚洲va中文字幕无码久久| 久久精品国产福利国产秒| 欧洲国产伦久久久久久久 | 人人狠狠综合88综合久久| 一本一本久久a久久综合精品蜜桃 一本一道久久综合狠狠老 | 久久国产三级无码一区二区| 久久久无码精品亚洲日韩蜜臀浪潮 | 18岁日韩内射颜射午夜久久成人| 好属妞这里只有精品久久| 伊人久久五月天| 伊人久久综在合线亚洲2019 | 久久久一本精品99久久精品88| 91精品国产91久久综合| 久久中文精品无码中文字幕| 久久狠狠高潮亚洲精品| 亚洲精品国产综合久久一线| 国产69精品久久久久777| 久久精品国产亚洲AV影院| 国产精品一区二区久久精品无码 | 久久久久久久亚洲精品| 久久国产亚洲精品麻豆| 亚洲成色WWW久久网站| 国产成人综合久久精品红| 精品久久久久国产免费| 99久久国产主播综合精品| 久久99精品久久久久久hb无码 | 狠狠色婷婷久久综合频道日韩| 人妻丰满?V无码久久不卡| 国产高清国内精品福利99久久| 国产精品岛国久久久久| 蜜臀av性久久久久蜜臀aⅴ| 99久久99久久精品国产片果冻| 狠狠色丁香久久婷婷综合蜜芽五月| 久久国产精品无码网站| 久久综合一区二区无码| 亚洲午夜精品久久久久久浪潮| 伊人精品久久久久7777|