2011-05-18 32 views
2

我已聲明變量:線程本地存儲(TLS) - 編譯器錯誤

static __thread int a;

我收到以下錯誤:

致命錯誤(DCC:1796):我怎樣才能解決這個__thread未在指定的目標環境

支持?我應該在make文件中啓用一些標誌嗎?

我在windriver編譯器(編譯powerpc)。我提到了類似的問題,但無法弄清楚。

基本上我試圖讓重入功能。任何建議都會有很大的幫助。

有什麼我可以通過包括pthread.h?

謝謝。

+0

您能否詳細說明您的重入要求? – 2011-05-18 14:34:54

回答

2

__thread是一個擴展。 POSIX線程接口來完成類似的事情pthread_getspecificpthread_setspecific

+0

謝謝。就我而言,你能告訴我一個簡單的例子嗎?無論我搜索什麼,這都太複雜了,因爲我第一次使用它。 – kp11 2011-05-18 11:47:51

+0

@kartik,tvn基本上告訴你這是如何工作的。但是,如果你真的想重新進入,所有這些可能不是一個好主意。在生成任何線程之前,您必須在'main'的某處執行'pthread_key_create'。 – 2011-05-18 14:33:06

3

__thread是gcc擴展,它不適用於所有平臺。正如上面提到的,你可以使用pthread_setspecific/pthread_getspecific,還有從人的例子:

  /* Key for the thread-specific buffer */ 
      static pthread_key_t buffer_key; 

      /* Once-only initialisation of the key */ 
      static pthread_once_t buffer_key_once = PTHREAD_ONCE_INIT; 

      /* Allocate the thread-specific buffer */ 
      void buffer_alloc(void) 
      { 
      pthread_once(&buffer_key_once, buffer_key_alloc); 
      pthread_setspecific(buffer_key, malloc(100)); 
      } 

      /* Return the thread-specific buffer */ 
      char * get_buffer(void) 
      { 
      return (char *) pthread_getspecific(buffer_key); 
      } 

      /* Allocate the key */ 
      static void buffer_key_alloc() 
      { 
      pthread_key_create(&buffer_key, buffer_destroy); 
      } 

      /* Free the thread-specific buffer */ 
      static void buffer_destroy(void * buf) 
      { 
      free(buf); 
      } 

但是,當我看到你正在試圖使折返功能,可重入函數不應該持有靜態非恆定的數據。

+0

你怎麼知道OP的需求/想要重入?事實上,'pthread_once'不是可重入的,但除非你打算從信號處理程序調用這個代碼,否則這是無關緊要的,即使這樣你也可能會使它不相關。 – 2011-05-18 12:54:21

+0

@ R .. OP說,不是嗎? – 2011-05-18 14:29:22

+1

我的壞,不知何故,我錯過了一條線。不過我會質疑OP是否真的需要重入。很多人不正確地使用這個詞來表示線程安全的。 – 2011-05-18 14:34:39