• <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++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            線程與私有數(shù)據(jù)

            Posted on 2012-03-20 18:44 Prayer 閱讀(997) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): LINUX/UNIX/AIX

            在多線程程序中,經(jīng)常要用全局變量來(lái)實(shí)現(xiàn)多個(gè)函數(shù)間的數(shù)據(jù)共享。由于數(shù)據(jù)空間是共享的,因此全局變量也為所有進(jìn)程共有。但有時(shí)應(yīng)用程序設(shè)計(jì)中必要提供線程私有的全局變量,這個(gè)變量?jī)H在線程中有效,但卻可以跨過(guò)多個(gè)函數(shù)訪問(wèn)。

            比如在程序里可能需要每個(gè)線程維護(hù)一個(gè)鏈表,而會(huì)使用相同的函數(shù)來(lái)操作這個(gè)鏈表,最簡(jiǎn)單的方法就是使用同名而不同變量地址的線程相關(guān)數(shù)據(jù)結(jié)構(gòu)。這樣的數(shù)據(jù)結(jié)構(gòu)可以由 Posix 線程庫(kù)維護(hù),成為線程私有數(shù)據(jù) (Thread-specific Data,或稱(chēng)為 TSD)。

            這里主要測(cè)試和線程私有數(shù)據(jù)有關(guān)的 4 個(gè)函數(shù):

            pthread_key_create();
            pthread_key_delete();

            pthread_getspecific();
            pthread_setspecific();

            程序代碼

            引用
            #include <stdio.h>
            #include <stdlib.h>
            #include <pthread.h>

            pthread_key_t key;

            struct test_struct {
                int i;
                float k;
            };


            void *child1 (void *arg)
            {
                struct test_struct struct_data;

                struct_data.i = 10;
                struct_data.k = 3.1415;

                pthread_setspecific (key, &struct_data);
                printf ("結(jié)構(gòu)體struct_data的地址為 0x%p/n", &(struct_data));
                printf ("child1 中 pthread_getspecific(key)返回的指針為:0x%p/n", (struct test_struct *)pthread_getspecific(key));

                printf ("利用 pthread_getspecific(key)打印 child1 線程中與key關(guān)聯(lián)的結(jié)構(gòu)體中成員值:/nstruct_data.i:%d/nstruct_data.k: %f/n", ((struct test_struct *)pthread_getspecific (key))->i, ((struct test_struct *)pthread_getspecific(key))->k);

                printf ("------------------------------------------------------/n");
            }

            void *child2 (void *arg)
            {
                int temp = 20;
                sleep (2);
                printf ("child2 中變量 temp 的地址為 0x%p/n",  &temp);
                pthread_setspecific (key, &temp);
                printf ("child2 中 pthread_getspecific(key)返回的指針為:0x%p/n", (int *)pthread_getspecific(key));
                printf ("利用 pthread_getspecific(key)打印 child2 線程中與key關(guān)聯(lián)的整型變量temp 值:%d/n", *((int *)pthread_getspecific(key)));
            }

            int main (void)
            {
                pthread_t tid1, tid2;

                pthread_key_create (&key, NULL);

                pthread_create (&tid1, NULL, (void *)child1, NULL);
                pthread_create (&tid2, NULL, (void *)child2, NULL);
                pthread_join (tid1, NULL);
                pthread_join (tid2, NULL);

                pthread_key_delete (key);

                return (0);
            }
            運(yùn)行與輸出
            引用
             ./pthread_key
            結(jié)構(gòu)體struct_data的地址為 0x0xb7699388
            child1 中 pthread_getspecific(key)返回的指針為:0x0xb7699388
            利用 pthread_getspecific(key)打印 child1 線程中與key關(guān)聯(lián)的結(jié)構(gòu)體中成員值:
            struct_data.i:10
            struct_data.k: 3.141500
            ------------------------------------------------------
            child2 中變量 temp 的地址為 0x0xb6e9838c
            child2 中 pthread_getspecific(key)返回的指針為:0x0xb6e9838c


            由輸出可見(jiàn),pthread_getspecific() 返回的是與key 相關(guān)聯(lián)數(shù)據(jù)的指針。需要注意的是,在利用這個(gè)返回的指針時(shí),它首先是 void 類(lèi)型的,它雖然指向關(guān)聯(lián)的數(shù)據(jù)地址處,但并不知道指向的數(shù)據(jù)類(lèi)型,所以在具體使用時(shí),要對(duì)其進(jìn)行強(qiáng)制類(lèi)型轉(zhuǎn)換。
            其次,兩個(gè)線程對(duì)自己的私有數(shù)據(jù)操作是互相不影響的。也就是說(shuō)哦,雖然 key 是同名且全局,但訪問(wèn)的內(nèi)存空間并不是相同的一個(gè)。key 就像是一個(gè)數(shù)據(jù)管理員,線程的私有數(shù)據(jù)只是到他那去注冊(cè),讓它知道你這個(gè)數(shù)據(jù)的存在。

             

             

            線程與私有數(shù)據(jù)例二
            程序代碼
            引用
            #include <malloc.h>
            #include <pthread.h>
            #include <stdio.h>
            #include <string.h>
            #include <stdlib.h>

            pthread_key_t thread_log_key;

            /*通用函數(shù)里可以利用 pthread_getspecific() 處理線程各自的私有數(shù)據(jù)*/
            void write_to_thread_log (const char *message)
            {
                FILE *thread_log = (FILE *)pthread_getspecific (thread_log_key);
                fprintf (thread_log, "%s/n", message);
            }

            void close_thread_log (void *thread_log)
            {
                fclose ((FILE *)thread_log);
            }

            void *thread_function (void *args)
            {
                char thread_log_filename[128];
                char thread_start_message[128];

                FILE *thread_log;

                sprintf (thread_log_filename, "thread%u.log", pthread_self());

                thread_log = fopen (thread_log_filename, "w");

                pthread_setspecific (thread_log_key, thread_log); //每個(gè)線程都設(shè)置自己的私有數(shù)據(jù)

                sprintf (thread_start_message, "thread %u starting", pthread_self());
                write_to_thread_log (thread_start_message); 

                pthread_exit(NULL);
            }

            int main()
            {
                int i;
                pthread_t threads[5];

                    /*創(chuàng)建私有數(shù)據(jù)鍵,close_thread_log 在線程退出時(shí)對(duì) key 關(guān)聯(lián)數(shù)據(jù)進(jìn)行清理*/
                pthread_key_create (&thread_log_key, close_thread_log); 
               
                for (i = 0; i < 5; i++)
                    pthread_create (&threads[i], NULL, thread_function, NULL); //創(chuàng)建多線程
               
                for (i = 0; i < 5; i++)
                    pthread_join (threads[i], NULL);  //等待各個(gè)線程結(jié)束

                return (0);
            }

            運(yùn)行與輸出
            引用
            ./pthread_key2
            ls *log
            thread3040865136.log  thread3049257840.log  thread3059047280.log  thread3067439984.log  thread3075832688.log
            cat thread3040865136.log
            thread 3040865136 starting



            久久久久亚洲av成人网人人软件 | 精品综合久久久久久97超人| 蜜臀av性久久久久蜜臀aⅴ| 精品久久8x国产免费观看| 国产成人无码精品久久久久免费| 日本加勒比久久精品| 久久国产亚洲高清观看| 青青青青久久精品国产h久久精品五福影院1421 | 天堂久久天堂AV色综合| 国产精品丝袜久久久久久不卡| 久久亚洲AV无码精品色午夜 | 亚洲综合婷婷久久| 久久精品国产99国产精品亚洲| 久久精品国内一区二区三区| 模特私拍国产精品久久| 9999国产精品欧美久久久久久 | 国产激情久久久久影院老熟女免费| 偷偷做久久久久网站| 久久亚洲av无码精品浪潮| 99精品久久精品| 久久综合综合久久综合| 一本色道久久88综合日韩精品 | 国产成人精品综合久久久| 久久久噜噜噜久久熟女AA片 | 大美女久久久久久j久久| 久久精品毛片免费观看| 久久中文字幕人妻熟av女| 久久人人爽人人爽人人片AV东京热| 日韩久久久久久中文人妻| 中文字幕无码久久久| 久久精品国产亚洲av瑜伽| 久久精品国产免费一区| 久久ZYZ资源站无码中文动漫| 久久精品桃花综合| 亚洲国产成人久久笫一页| 久久九九久精品国产| 久久久久99精品成人片| 久久国产三级无码一区二区| 精品久久久无码中文字幕天天| 国产精品久久久久久久久久免费| 国产福利电影一区二区三区久久老子无码午夜伦不 |