TSD(Thread Specific Data)線程專有資料

來源:互聯網
上載者:User

(1)全域變數

(2)局部變數

(3)TSD(Thread-Specific Data 線程專有資料)

1.http://upczap.itpub.net/

   在單線程的程式裡,有兩種基本的資料:全域變數和局部變數。但在多線程程式裡,還有第三種資料類型:線程資料(TSD: Thread-Specific Data)。它和全域變數很象,線上程內部,各個函數可以象使用全域變數一樣調用它,但它對線程外部的其它線程是不可見的。這種資料的必要性是顯而易見 的。例如我們常見的變數errno,它返回標準的出錯資訊。它顯然不能是一個局部變數,幾乎每個函數都應該可以調用它;但它又不能是一個全域變數,否則在 A線程裡輸出的很可能是B線程的出錯資訊。要實現諸如此類的變數,我們就必須使用線程資料。我們為每個線程資料建立一個鍵,它和這個鍵相關聯,在各個線程 裡,都使用這個鍵來指代線程資料,但在不同的線程裡,這個鍵代表的資料是不同的,在同一個線程裡,它代表同樣的資料內容。



2.http://bbs.chinaunix.net/viewthread.php?tid=941730

linux 多線程編程中引入了Thread-Specific Data(線程相關的資料)的概念
為什麼需要"線程相關的資料"呢?怎樣使用"線程相關的資料"呢?

1. 為什麼需要Thread-Specific Data "線程相關的資料"

這裡只介紹我個人認為的一個原因, 當然它還有許多其他用途,歡迎大家討論

例子:實現同時運行兩個線程,對於每個線程,在該線程調用的每個函數中列印線程的名字,以及它正在調用的函數的名字.

(下面的例子與實現只是為了說明問題,有些地方可能不妥)

不使用"線程相關的資料"的兩種實現方法:

實現方法1. 不使用全域變數 #include <string.h>
#include <pthread.h>
#define MAXLENGTH 20

void another_func (const char * threadName)
{
  printf ("%s is running in another_func/n", threadName);
}

void * thread_func (void * args)
{
  char threadName[MAXLENGTH];
  strncpy (threadName, (char *)args, MAXLENGTH-1);
  
  printf ("%s is running in thread_func/n", threadName);
  another_func (threadName);
  
}

int main (int argc, char * argv[])
{

  pthread_t pa, pb;
  pthread_create ( &pa, NULL, thread_func, "Thread A");
  pthread_create ( &pb, NULL, thread_func, "Thread B");

  pthread_join (pa, NULL);
  pthread_join (pb, NULL);
}


輸出結果為:
Thread A is running in thread_func
Thread A is running in another_func
Thread B is running in thread_func
Thread B is running in another_func

該方法的缺點是:由於要記錄是哪一個線程在調用函數,每個函數需要一個額外的參數來
記錄線程的名字,例如another_func函數需要一個threadName參數
如果調用的函數多了,則每一個都需要一個這樣的參數

實現方法2. 使用全域變數 #include <string.h>
#include <pthread.h>
#define MAXLENGTH 20

char threadName[MAXLENGTH];
pthread_mutex_t sharedMutex=PTHREAD_MUTEX_INITIALIZER;

void another_func ()
{
  printf ("%s is running in another_func/n", threadName);
}

void * thread_func (void * args)
{
  pthread_mutex_lock(&sharedMutex);
  strncpy (threadName, (char *)args, MAXLENGTH-1);
  printf ("%s is running in thread_func/n", threadName);
  another_func ();
  pthread_mutex_unlock(&sharedMutex);
  
}

int main (int argc, char * argv[])
{

  pthread_t pa, pb;
  pthread_create ( &pa, NULL, thread_func, "Thread A");
  pthread_create ( &pb, NULL, thread_func, "Thread B");

  pthread_join (pa, NULL);
  pthread_join (pb, NULL);
}


該方法的缺點是:由於多個線程需要讀寫全域變數threadName,就需要使用互斥機制

分析以上兩種實現方法,Thread-Specific Data "線程相關的資料"的一個好處就體現出來了:
(1)"線程相關的資料"可以是一個全域變數,並且
(2)每個線程存取的"線程相關的資料"是相互獨立的.


2. 怎樣使用"線程相關的資料"

這是利用"線程相關的資料"的實現方式: #include <string.h>
#include <pthread.h>

pthread_key_t p_key;

void another_func ()
{
  printf ("%s is running in another_func/n", (char *)pthread_getspecific(p_key));
}

void * thread_func (void * args)
{
  pthread_setspecific(p_key, args);
  printf ("%s is running in thread_func/n", (char *)pthread_getspecific(p_key));
  another_func ();
  
}

int main (int argc, char * argv[])
{

  pthread_t pa, pb;

  pthread_key_create(&p_key, NULL);
  
  pthread_create ( &pa, NULL, thread_func, "Thread A");
  pthread_create ( &pb, NULL, thread_func, "Thread B");

  pthread_join (pa, NULL);
  pthread_join (pb, NULL);
}


說明:
(1)
線程A, B共用了p_key,
通過p_key,就可以存取只跟當前線程相關的一個值(這個值由編譯器管理)
線程A----->p_key----->線程A相關的值(由編譯器管理)
線程B----->p_key----->線程B相關的值(由編譯器管理)

設定"線程相關的資料",使用
int pthread_setspecific(pthread_key_t key, const void *pointer);
讀取"線程相關的資料",使用
void * pthread_getspecific(pthread_key_t key);

注意到,這兩個函數分別有一個void類型的指標,我們的線程就是通過這兩個指標分別與
"線程相關的資料"的資料進行互動的

(2)
由於p_key是一個全域變數,
函數another_func不需要額外的參數就可以訪問它;
又因為它是"線程相關的資料", 線程A, B通過p_key存取的資料是相互獨立的,
這樣就不需要額外的互斥機制來保證資料訪問的正確性了.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.