搜索
熱搜: 活動 交友 discuz
查看: 2903|回復: 0
打印 上一主題 下一主題

[教學] C++多執行緒(九)

[複製鏈接]
跳轉到指定樓層
1#
發表於 2007-8-14 04:44:51 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
  1. 多執行緒使用總結

  2. 一 執行緒局部存儲 (TLS)
  3.       來自:[url]http://msdn2.microsoft.com/en-us/library/ms686749.aspx[/url]
  4.       
  5.       同一進程中的所有執行緒共享相同的虛擬地址空間。不同的執行緒中的局部變量有不同的副本,但是static和globl變量是同一進程中的所有執行緒共享的。使用TLS技術可以為static和globl的變量,根據當前進程的執行緒數量創建一個array,每個執行緒可以通過array的index來訪問對應的變量,這樣也就保證了static和global的變量為每一個執行緒都創建不同的副本。

  6. 二 執行緒局部存儲(TLS)實現使用
  7. 1)TLS 的 API 實現
  8. 通過 Win32 API 層和編譯器實現「執行緒本地存儲」。有關詳細信息,請參見 Win32 API 文檔中的 TlsAlloc、TlsGetValue、TlsSetValue 和 TlsFree。 (下面的代碼對msdn的稍加修改)
  9. #include <windows.h>
  10. #include <stdio.h>

  11. #define THREADCOUNT 10
  12. DWORD dwTlsIndex;  

  13. static
  14. int g_x =
  15. 100;  // test static var for multiple threading

  16. VOID ErrorExit(LPSTR);

  17. VOID CommonFunc(VOID)
  18. {
  19.     LPVOID lpvData;

  20.     // Retrieve a data pointer for the current thread.
  21.     lpvData = TlsGetValue(dwTlsIndex);
  22.     if ((lpvData ==
  23. 0) && (GetLastError() != ERROR_SUCCESS))
  24.         ErrorExit("TlsGetValue error");
  25.     int
  26. *pg_x = (int*)lpvData;

  27.     // Use the data stored for the current thread.
  28.     printf("thread %d: g_x adress=%lx,g_x copy ++ = %d\n"、GetCurrentThreadId()、pg_x、*pg_x);
  29.     Sleep(1000);
  30. }

  31. DWORD WINAPI ThreadFunc(VOID)
  32. {
  33.     LPVOID lpvData;

  34.     // Initialize the TLS index for this thread.
  35.     lpvData = (LPVOID) LocalAlloc(LPTR、256);
  36.     //*(int*)lpvData = g_x;

  37. int
  38. *pg_x = (int*)lpvData;
  39.     *pg_x = g_x;
  40.     if (! TlsSetValue(dwTlsIndex、lpvData))
  41.         ErrorExit("TlsSetValue error");     

  42.     printf("thread %d: g_x adress=%lx,g_x copy = %d\n"、GetCurrentThreadId()、pg_x、*pg_x);

  43.     InterlockedExchangeAdd(reinterpret_cast<long*>(pg_x),1);
  44.     CommonFunc();

  45.     // Release the dynamic memory before the thread returns.
  46.     lpvData = TlsGetValue(dwTlsIndex);
  47.     if (lpvData !=
  48. 0)
  49.         LocalFree((HLOCAL) lpvData);

  50.     return
  51. 0;
  52. }

  53. int main(VOID)
  54. {
  55.     DWORD IDThread;
  56.     HANDLE hThread[THREADCOUNT];
  57.     int i;

  58.     printf("main thread: g_x is :%d\n",g_x);

  59.     // Allocate a TLS index.  

  60. if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES)
  61.         ErrorExit("TlsAlloc failed");

  62.     //test for multiple static or global var

  63. int dwTlsIndex2 = TlsAlloc();

  64.     // Create multiple threads.

  65. for (i =
  66. 0; i < THREADCOUNT; i++)
  67.     {
  68.         hThread = CreateThread(NULL、// default security attributes

  69. 0,                           // use default stack size
  70.             (LPTHREAD_START_ROUTINE) ThreadFunc、// thread function
  71.             NULL,                    // no thread function argument

  72. 0,                       // use default creation flags

  73. &IDThread);              // returns thread identifier

  74.         // Check the return value for success.

  75. if (hThread == NULL)
  76.             ErrorExit("CreateThread error\n");
  77.     }

  78.     for (i =
  79. 0; i < THREADCOUNT; i++)
  80.         WaitForSingleObject(hThread、INFINITE);

  81.     TlsFree(dwTlsIndex);

  82.     printf("main thread: g_x is :%d\n",g_x);
  83.     return
  84. 0;
  85. }

  86. VOID ErrorExit (LPSTR lpszMessage)
  87. {
  88.     fprintf(stderr、"%s\n"、lpszMessage);
  89.     ExitProcess(0);
  90. }

  91. 運行結果:(可以看出不同的執行緒中的g_x變量有不同的地址,即有不同的拷貝,主執行緒變量在開始和最後都不被改變。)

  92. PS: (TLS的實現原理與API解釋)
  93. 1:在多執行緒的進程中,為每一個static或是global變量創建一個void*的數組,使變量不同執行緒都有一個拷貝,然後拷貝的指針放入Void*的數組中。
  94. 2:TlsAlloc() 得到對應於一個static或global變量所對應的void*的數組的索引,這個用來標示不同static或global變量所對應的void*的數組。
  95. 3:LocalAlloc()用來分配變量在此執行緒的拷貝的指針。
  96. 4:TlsSetValue()用來把剛分配的指針加到所對應的void*的數組中,加入後一般對會對此指針賦值供此執行緒使用。
  97. 5:TlsGetValue()用來從對應的void*的數組中找到此執行緒所對應的拷貝的指針。
  98. 6:   TlsFree() 釋放整個void*的指針數組。


  99. 2)TLS 的編譯器實現

  100. 為了支援 TLS,已將新屬性 thread 添加到了 C 和 C++ 語言,並由 Visual C++ 編譯器支援。使用 __declspec 關鍵字聲明 thread 變量。例如,以下代碼聲明了一個整數執行緒局部變量,並用一個值對其進行初始化:

  101. __declspec( thread ) int tls_i = 1;

  102. 下面的代碼使用了VC提供的__declspec關鍵字來實現TLS,如果不使用TLS,全局變量g_x則線上程中輸出的結果都是1,2,3,4.。。。但是如果使用TLS則線上程中輸出的結果都是1,編譯器幫我們保證了每個執行緒都有一個副本。 我們還可以看到主執行緒在開始和最後輸出的g_x都是0,這也更說明了線上程有不同的副本。
  103. #include <windows.h>
  104. #include <stdio.h>

  105. #define THREADCOUNT 10

  106. DWORD dwTlsIndex;

  107. //static int g_x = 0;

  108. #define Thread  __declspec(thread)
  109. Thread static
  110. int g_x =
  111. 0;

  112. VOID ErrorExit(LPSTR);

  113. DWORD WINAPI ThreadFunc(VOID)
  114. {
  115.   InterlockedExchangeAdd(reinterpret_cast<long*>(&g_x),1); //g_x+=1;   
  116.   printf("thread id: %d、g_x++ = %d、g_x adress = %d\n",GetCurrentThreadId(),g_x,&g_x);
  117.   return
  118. 1;
  119. }

  120. int main(VOID)
  121. {
  122.    DWORD IDThread;
  123.    HANDLE hThread[THREADCOUNT];
  124.    int i;

  125.     printf("main thread: g_x = %d、g_x adress = %d\n",g_x,&g_x);
  126.   
  127. // Create multiple threads.  

  128. for (i =
  129. 0; i < THREADCOUNT; i++)
  130.    {
  131.       hThread = CreateThread(NULL、// default security attributes

  132. 0,                           // use default stack size
  133.          (LPTHREAD_START_ROUTINE) ThreadFunc、// thread function
  134.          NULL,                    // no thread function argument

  135. 0,                       // use default creation flags

  136. &IDThread);              // returns thread identifier

  137.    // Check the return value for success.

  138. if (hThread == NULL)
  139.          ErrorExit("CreateThread error\n");
  140.    }

  141.    for (i =
  142. 0; i < THREADCOUNT; i++)
  143.       WaitForSingleObject(hThread、INFINITE);
  144.   

  145.    printf("main thread: g_x = %d、g_x adress = %d\n",g_x,&g_x);
  146.    return
  147. 0;
  148. }

  149. VOID ErrorExit (LPSTR lpszMessage)
  150. {
  151.    fprintf(stderr、"%s\n"、lpszMessage);
  152.    ExitProcess(0);

  153. 運行結果:

  154. 三 使用VC關鍵字實現TLS需要注意:

  155. 聲明靜態綁定執行緒的本地對像和變量時必須遵守下列原則:

  156. thread 屬性只能應用於資料聲明和定義。它不能用於函數聲明或定義。例如,以下代碼將生成一個編譯器錯誤:    #define Thread  __declspec( thread )
  157.     Thread void func();     // This will generate an error.
  158. 只能在具有 static 作用域的資料項上指定 thread 修飾符。包括全局資料對像(包括 static 和 extern)、本地靜態對像和 C++ 類的靜態資料成員。不可以用 thread 屬性聲明自動資料對象。以下代碼將生成編譯器錯誤:    #define Thread  __declspec( thread )
  159.     void func1()
  160.     {
  161.     Thread int tls_i;            // This will generate an error.
  162.     }
  163.     int func2( Thread int tls_i )    // This will generate an error.
  164.     {
  165.     return tls_i;
  166.     }
  167. 執行緒本地對象的聲明和定義必須全都指定 thread 屬性。例如,以下代碼將生成錯誤:    #define Thread  __declspec( thread )
  168.     extern int tls_i;        // This will generate an error、since the
  169.     int Thread tls_i;        // declaration and definition differ.
  170. thread 屬性不能用作類型修飾符。例如,以下代碼將生成一個編譯器錯誤:    char __declspec( thread ) *ch;        // Error
  171. C++ 類不能使用 thread 屬性。但是,可以使用 thread 屬性將 C++ 類對像實例化。例如,以下代碼將生成一個編譯器錯誤:    #define Thread  __declspec( thread )
  172.     class Thread C       // Error: classes cannot be declared Thread.
  173.     {
  174.     // Code
  175.     };
  176.     C CObject;    因為允許使用 thread 屬性的 C++ 對象的聲明,因此下面兩個示例在語義上是等效的:   
  177. #define Thread  __declspec( thread )
  178.     Thread class B
  179.     {
  180.     // Code
  181.     } BObject;               // OK--BObject is declared thread local.
  182.     class B
  183.     {
  184.     // Code
  185.     };
  186.     Thread B BObject;        // OK--BObject is declared thread local.
  187. 不將執行緒本地對象的地址視為常數,並且涉及此類地址的任何表達式都不視為常數。在標準 C 中,這種作法的效果是禁止將執行緒本地變量的地址用作對像或指針的初始值設定項。例如,C 編譯器將以下代碼標記為錯誤:    #define Thread  __declspec( thread )
  188.     Thread int tls_i;
  189.     int *p = &tls_i;       //This will generate an error in C.    但是,此限制不適用於 C++。因為 C++允許動態初始化所有對象,因此可以用使用執行緒本地變量地址的表達式初始化對象。實現此操作的方式與實現執行緒本地對像結構的方式相同。例如,以上顯示的代碼在作為 C++ 源檔案編譯時不會生成錯誤。請注意:只有在其中獲取地址的執行緒仍然存在的情況下,執行緒本地變量的地址才有效。

  190. 標準 C 允許使用涉及引用自身的表達式初始化對像或變量,但只適用於非靜態作用域的對象。雖然 C++ 通常允許使用涉及引用自身的表達式動態初始化對象,但是這種類型的初始化不允許用於執行緒本地對象。例如:    #define Thread  __declspec( thread )
  191.     Thread int tls_i = tls_i;                // Error in C and C++
  192.     int j = j;                               // OK in C++、error in C
  193.     Thread int tls_i = sizeof( tls_i )       // Legal in C and C++    請注意:包含正在初始化的對象的 sizeof 表達式不建立對自身的引用且在 C 和 C++ 中都是合法的。   
  194. C++ 不允許此類對執行緒資料的動態初始化,因為將來可能要對執行緒本地存儲功能進行增強。

  195. 如果 DLL 將任何非本地資料或對像聲明為 __declspec(執行緒),動態加載該 DLL 時會導致保護錯誤。使用 LoadLibrary 加載所有 DLL 後,每當代碼引用非本地 __declspec(執行緒)資料時,將導致系統故障。由於執行緒的全局變量空間是在運行時分配的,因此此空間的大小是以應用程序的需求和所有靜態鏈接的 DLL 的需求相加為基礎計算出來的。使用 LoadLibrary 時,無法擴展此空間以允許放置用 __declspec(執行緒)聲明的執行緒本地變量。如果 DLL 可能是用 LoadLibrary 加載的,請在 DLL 中使用 TLS API(如 TlsAlloc)來分配 TLS。
  196. 四  DLL使用TLS :[url]http://msdn2.microsoft.com/en-us/library/ms686997.aspx[/url]


複製代碼
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

本論壇為非營利之網路平台,所有文章內容均為網友自行發表,不代表論壇立場!若涉及侵權、違法等情事,請告知版主處理。


Page Rank Check

廣告刊登  |   交換連結  |   贊助我們  |   服務條款  |   免責聲明  |   客服中心  |   中央分站

手機版|中央論壇

GMT+8, 2026-6-4 10:33 , Processed in 0.032586 second(s), 17 queries .

Powered by Discuz!

© 2005-2015 Copyrights. Set by YIDAS

快速回復 返回頂部 返回列表