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

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

【Linux系统编程】线程私有数据

發(fā)布時(shí)間:2024/4/24 linux 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Linux系统编程】线程私有数据 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

00. 目錄

文章目錄

    • 00. 目錄
    • 01. 線(xiàn)程之間共享數(shù)據(jù)
    • 02. 線(xiàn)程私有數(shù)據(jù)
      • 2.1 創(chuàng)建線(xiàn)程私有數(shù)據(jù)
      • 2.2 銷(xiāo)毀線(xiàn)程私有數(shù)據(jù)
      • 2.3 關(guān)聯(lián)線(xiàn)程私有數(shù)據(jù)成員
      • 2.4 讀取線(xiàn)程私有數(shù)據(jù)所關(guān)聯(lián)的值
    • 03. 案例實(shí)踐
    • 04. 附錄

01. 線(xiàn)程之間共享數(shù)據(jù)

在多線(xiàn)程程序中,經(jīng)常要用全局變量來(lái)實(shí)現(xiàn)多個(gè)函數(shù)間的數(shù)據(jù)共享。由于數(shù)據(jù)空間是共享的,因此全局變量也為所有線(xiàn)程共有。

測(cè)試代碼:

#include <stdio.h> #include <pthread.h> #include <unistd.h> #include <stdlib.h>int key = 100; //全局變量void *helloworld_one(void *arg) {printf("the message is %s\n",(char *)arg);key = 10;printf("key=%d, the child id is %lu\n", key, pthread_self());return NULL; }void *helloworld_two(void *arg) {printf("the message is %s\n", (char *)arg);sleep(1);printf("key=%d, the child id is %lu\n", key, pthread_self());return NULL; }int main(int argc, char *argv[]) {pthread_t thread_id_one;pthread_t thread_id_two;//創(chuàng)建線(xiàn)程pthread_create(&thread_id_one, NULL, helloworld_one, "helloworld_one");pthread_create(&thread_id_two, NULL, helloworld_two, "helloworld_two");//等待線(xiàn)程結(jié)束,回收資源pthread_join(thread_id_one, NULL);pthread_join(thread_id_two, NULL);return 0; }

測(cè)試結(jié)果:

deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ gcc 1.c -pthread deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ ./a.out the message is helloworld_one key=10, the child id is 139954627110656 the message is helloworld_two key=10, the child id is 139954618717952 deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$

由運(yùn)行結(jié)果可以看出,其中一個(gè)線(xiàn)程對(duì)全局變量的修改將影響到另一個(gè)線(xiàn)程的訪(fǎng)問(wèn)。

02. 線(xiàn)程私有數(shù)據(jù)

但有時(shí)應(yīng)用程序設(shè)計(jì)中必要提供線(xiàn)程私有的全局變量,這個(gè)變量?jī)H在線(xiàn)程中有效,但卻可以跨過(guò)多個(gè)函數(shù)訪(fǎng)問(wèn)。比如在程序里可能需要每個(gè)線(xiàn)程維護(hù)一個(gè)鏈表,而會(huì)使用相同的函數(shù)來(lái)操作這個(gè)鏈表,最簡(jiǎn)單的方法就是使用同名而不同變量地址的線(xiàn)程相關(guān)數(shù)據(jù)結(jié)構(gòu)。這樣的數(shù)據(jù)結(jié)構(gòu)可以由 Posix 線(xiàn)程庫(kù)維護(hù),成為線(xiàn)程私有數(shù)據(jù) (Thread-specific Data,或稱(chēng)為 TSD)。

2.1 創(chuàng)建線(xiàn)程私有數(shù)據(jù)

int pthread_key_create(pthread_key_t *key, void (*destructor)(void*)); 功能:創(chuàng)建一個(gè)類(lèi)型為 pthread_key_t 類(lèi)型的私有數(shù)據(jù)變量( key )。 參數(shù):key:在分配( malloc )線(xiàn)程私有數(shù)據(jù)之前,需要?jiǎng)?chuàng)建和線(xiàn)程私有數(shù)據(jù)相關(guān)聯(lián)的鍵( key ),這個(gè)鍵的功能是獲得對(duì)線(xiàn)程私有數(shù)據(jù)的訪(fǎng)問(wèn)權(quán)。destructor:清理函數(shù)名字( 如:fun )。當(dāng)線(xiàn)程退出時(shí),如果線(xiàn)程私有數(shù)據(jù)地址不是非 NULL,此函數(shù)會(huì)自動(dòng)被調(diào)用。該函數(shù)指針可以設(shè)成 NULL ,這樣系統(tǒng)將調(diào)用默認(rèn)的清理函數(shù)。回調(diào)函數(shù)其定義如下:void fun(void *arg){// arg 為 key 值} 返回值:成功:0失敗:非 0

不論哪個(gè)線(xiàn)程調(diào)用 pthread_key_create(),所創(chuàng)建的 key 都是所有線(xiàn)程可訪(fǎng)問(wèn),但各個(gè)線(xiàn)程可根據(jù)自己的需要往 key 中填入不同的值,相當(dāng)于提供了一個(gè)同名不同值的變量。

2.2 銷(xiāo)毀線(xiàn)程私有數(shù)據(jù)

int pthread_key_delete(pthread_key_t key); 功能:注銷(xiāo)線(xiàn)程私有數(shù)據(jù)。這個(gè)函數(shù)并不會(huì)檢查當(dāng)前是否有線(xiàn)程正使用線(xiàn)程私有數(shù)據(jù)( key ),也不會(huì)調(diào)用清理函數(shù) destructor() ,而只是將線(xiàn)程私有數(shù)據(jù)( key )釋放以供下一次調(diào)用pthread_key_create() 使用。 參數(shù):key:待注銷(xiāo)的私有數(shù)據(jù)。 返回值:成功:0失敗:非 0

2.3 關(guān)聯(lián)線(xiàn)程私有數(shù)據(jù)成員

int pthread_setspecific(pthread_key_t key, const void *value); 功能:設(shè)置線(xiàn)程私有數(shù)據(jù)( key ) 和 value 關(guān)聯(lián),注意,是 value 的值(不是所指的內(nèi)容)和 key 相關(guān)聯(lián)。 參數(shù):key:線(xiàn)程私有數(shù)據(jù)。value:和 key 相關(guān)聯(lián)的指針。 返回值:成功:0失敗:非 0

2.4 讀取線(xiàn)程私有數(shù)據(jù)所關(guān)聯(lián)的值

void *pthread_getspecific(pthread_key_t key); 功能:讀取線(xiàn)程私有數(shù)據(jù)( key )所關(guān)聯(lián)的值。 參數(shù):key:線(xiàn)程私有數(shù)據(jù)。 返回值:成功:線(xiàn)程私有數(shù)據(jù)( key )所關(guān)聯(lián)的值。失敗:NULL

03. 案例實(shí)踐

測(cè)試程序

// this is the test code for pthread_key #include <stdio.h> #include <pthread.h> pthread_key_t key; // 私有數(shù)據(jù),全局變量void echomsg(void *t) { printf("[destructor] thread_id = %lu, param = %p\n", pthread_self(), t); } void *child1(void *arg) { int i = 10;pthread_t tid = pthread_self(); //線(xiàn)程號(hào)printf("\nset key value %d in thread %lu\n", i, tid); pthread_setspecific(key, &i); // 設(shè)置私有數(shù)據(jù)printf("thread one sleep 2 until thread two finish\n\n");sleep(2); printf("\nthread %lu returns %d, add is %p\n",tid, *((int *)pthread_getspecific(key)), pthread_getspecific(key) ); } void *child2(void *arg) { int temp = 20;pthread_t tid = pthread_self(); //線(xiàn)程號(hào)printf("\nset key value %d in thread %lu\n", temp, tid); pthread_setspecific(key, &temp); //設(shè)置私有數(shù)據(jù)sleep(1); printf("thread %lu returns %d, add is %p\n", tid, *((int *)pthread_getspecific(key)), pthread_getspecific(key)); } int main(void) { pthread_t tid1,tid2; pthread_key_create(&key, echomsg); // 創(chuàng)建pthread_create(&tid1, NULL, child1, NULL); pthread_create(&tid2, NULL, child2, NULL); pthread_join(tid1, NULL);pthread_join(tid2, NULL);pthread_key_delete(key); // 注銷(xiāo)return 0; }

執(zhí)行結(jié)果:

deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$ ./a.out set key value 10 in thread 140551366268672set key value 20 in thread 140551357875968 thread one sleep 2 until thread two finishthread 140551357875968 returns 20, add is 0x7fd4a9c23ecc [destructor] thread_id = 140551357875968, param = 0x7fd4a9c23eccthread 140551366268672 returns 10, add is 0x7fd4aa424ecc [destructor] thread_id = 140551366268672, param = 0x7fd4aa424ecc deng@itcast:/mnt/hgfs/LinuxHome/code.bak2$

從運(yùn)行結(jié)果來(lái)看,各線(xiàn)程對(duì)自己的私有數(shù)據(jù)操作互不影響。也就是說(shuō),雖然 key 是同名且全局,但訪(fǎng)問(wèn)的內(nèi)存空間并不是同一個(gè)。

04. 附錄

總結(jié)

以上是生活随笔為你收集整理的【Linux系统编程】线程私有数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。