1 #include<Windows.h> 2 #include<iostream> 3 using namespace std; 4 _declspec(thread) int g_number = 100; 5 HANDLE hEvent1 = CreateEvent(NULL, FALSE, TRUE, L"TLSTEST"); 6 HANDLE hEvent2 = CreateEvent(NULL, FALSE, false, L"TLSTEST"); 7 DWORD WINAPI threadProc1(LPVOID lparam) 8 { 9 WaitForSingleObject(hEvent1, -1); 10 g_number = 200; 11 cout << "threadProc1 g_number = " << g_number << endl; 12 SetEvent(hEvent2); 13 return 0; 14 } 15 DWORD WINAPI threadProc2(LPVOID lparam) 16 { 17 WaitForSingleObject(hEvent2, -1); 18 cout << "threadProc2 g_number = " << g_number << endl; 19 return 0; 20 } 21 22 int main() 23 { 24 25 HANDLE hThread1 = CreateThread(NULL, NULL, threadProc1, NULL, NULL, NULL); 26 HANDLE hThread2 = CreateThread(NULL, NULL, threadProc2, NULL, NULL, NULL); 27 WaitForSingleObject(hThread1,-1); 28 WaitForSingleObject(hThread2,-1); 29 system("pause"); 30 CloseHandle(hEvent1); 31 CloseHandle(hEvent2); 32 CloseHandle(hThread1); 33 CloseHandle(hThread2); 34 return 0; 35 }