https://github.com/gongluck/Windows-Core-Program.gitc++
//第8章 用戶模式下的線程同步.cpp: 定義應用程序的入口點。 // #include "stdafx.h" #include "第8章 用戶模式下的線程同步.h" LONG g_i = 100; LONG g_b = FALSE; CRITICAL_SECTION g_cs; //關鍵段 SRWLOCK g_rw; //讀寫鎖 CONDITION_VARIABLE g_cv; //條件變量 DWORD WINAPI Thread1(PVOID param) { for (int i = 0; i < 100; ++i) { EnterCriticalSection(&g_cs); g_i += i; LeaveCriticalSection(&g_cs); } return 0; } DWORD WINAPI Thread2(PVOID param) { for (int i = 0; i < 100; ++i) { if (TryEnterCriticalSection(&g_cs)) { g_i += i; LeaveCriticalSection(&g_cs); } else SwitchToThread(); } return 0; } DWORD WINAPI Thread3(PVOID param) { for (int i = 0; i < 100; ++i) { AcquireSRWLockExclusive(&g_rw); //寫 OutputDebugString(TEXT("------------AcquireSRWLockExclusive succeed.\n")); ReleaseSRWLockExclusive(&g_rw); } return 0; } DWORD WINAPI Thread4(PVOID param) { for (int i = 0; i < 100; ++i) { AcquireSRWLockShared(&g_rw); //讀 OutputDebugString(TEXT("------------AcquireSRWLockShared succeed.\n")); ReleaseSRWLockShared(&g_rw); } return 0; } DWORD WINAPI Thread5(PVOID param) { int n = 0; for (int i = 0; i < 100; ++i) { EnterCriticalSection(&g_cs); ++n; if (n >= 100) break; if(g_i <= 0) SleepConditionVariableCS(&g_cv, &g_cs, INFINITE); //解鎖等待條件變量,返回時再加鎖 g_i--; LeaveCriticalSection(&g_cs); } return 0; } DWORD WINAPI Thread6(PVOID param) { for (int i = 0; i < 100; ++i) { if (TryEnterCriticalSection(&g_cs)) { g_i++; WakeConditionVariable(&g_cv); //喚醒等待條件變量的線程 LeaveCriticalSection(&g_cs); } else SwitchToThread(); } return 0; } int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { //原子操做Interlocked系列 LONG newl = InterlockedAdd(&g_i, 1); LONG oldl = InterlockedExchange(&g_i, 10); oldl = InterlockedCompareExchange(&g_i, 100, 10); LONGLONG oldll = InterlockedAnd(&g_i, 0x0001); //旋轉鎖! while (InterlockedExchange(&g_b, TRUE) == TRUE) Sleep(0); //使用關鍵段 //InitializeCriticalSection(&g_cs); BOOL bret = InitializeCriticalSectionAndSpinCount(&g_cs, 1);//初始化關鍵段並用上旋轉鎖 oldl = SetCriticalSectionSpinCount(&g_cs, 4000); g_i = 0; HANDLE hthread1 = CreateThread(nullptr, 0, Thread1, nullptr, 0, nullptr); HANDLE hthread2 = CreateThread(nullptr, 0, Thread2, nullptr, 0, nullptr); WaitForSingleObject(hthread1, INFINITE); WaitForSingleObject(hthread2, INFINITE); CloseHandle(hthread1); hthread1 = nullptr; CloseHandle(hthread2); hthread2 = nullptr; //使用讀寫鎖 InitializeSRWLock(&g_rw); HANDLE hthread3 = CreateThread(nullptr, 0, Thread3, nullptr, 0, nullptr); HANDLE hthread4 = CreateThread(nullptr, 0, Thread4, nullptr, 0, nullptr); WaitForSingleObject(hthread3, INFINITE); WaitForSingleObject(hthread4, INFINITE); CloseHandle(hthread3); hthread3 = nullptr; CloseHandle(hthread4); hthread4 = nullptr; //條件變量(誤入鎖?) InitializeConditionVariable(&g_cv); g_i = 0; HANDLE hthread5 = CreateThread(nullptr, 0, Thread5, nullptr, 0, nullptr); HANDLE hthread6 = CreateThread(nullptr, 0, Thread6, nullptr, 0, nullptr); WaitForSingleObject(hthread5, INFINITE); WaitForSingleObject(hthread6, INFINITE); CloseHandle(hthread5); hthread5 = nullptr; CloseHandle(hthread6); hthread6 = nullptr; DeleteCriticalSection(&g_cs); system("pause"); return 0; }