读写锁
#include <Windows.h> #include <tchar.h> #include <stdio.h> #include <process.h> #include <winbase.h> // 读写锁 SRWLOCK srwLock; unsigned WINAPI ReadThread1(LPVOID) { while(1) { AcquireSRWLockShared(&srwLock); printf("read 1111\n"); ReleaseSRWLockShared(&srwLock); Sleep(500); } return 0; } unsigned WINAPI ReadThread2(LPVOID) { while(1) { AcquireSRWLockShared(&srwLock); printf("read 2222222222\n"); ReleaseSRWLockShared(&srwLock); Sleep(500); } return 0; } unsigned WINAPI WriteThread(LPVOID) { while(1) { AcquireSRWLockExclusive(&srwLock); printf("write\n"); ReleaseSRWLockExclusive(&srwLock); Sleep(1000); } return 0; } int main() { InitializeSRWLock(&srwLock); CloseHandle( (HANDLE)_beginthreadex(NULL, 0, ReadThread1, NULL, 0, NULL) ); CloseHandle( (HANDLE)_beginthreadex(NULL, 0, ReadThread2, NULL, 0, NULL) ); CloseHandle( (HANDLE)_beginthreadex(NULL, 0, WriteThread, NULL, 0, NULL) ); system("pause"); return 0; }
条件变量
SleepConditionVariableSRW 可以递归调用,但是最后需要释放一次
#include <Windows.h> #include <tchar.h> #include <stdio.h> #include <process.h> #include <winbase.h> // 读写锁和条件变量 // 关键段和条件变量 CRITICAL_SECTION g_cs; CONDITION_VARIABLE g_conditionVariable; unsigned WINAPI TestCS(LPVOID) { EnterCriticalSection(&g_cs); printf("11\n"); SleepConditionVariableCS(&g_conditionVariable,&g_cs,INFINITE); printf("aaaa\n"); LeaveCriticalSection(&g_cs); return 0; } unsigned WINAPI ttttttt(LPVOID) { EnterCriticalSection(&g_cs); printf("2222\n"); printf("333\n"); LeaveCriticalSection(&g_cs); WakeConditionVariable(&g_conditionVariable); return 0; } int main() { InitializeCriticalSection(&g_cs); InitializeConditionVariable(&g_conditionVariable); HANDLE hThread = (HANDLE)_beginthreadex(NULL, 0, TestCS, NULL, 0, NULL); HANDLE hThread1 = (HANDLE)_beginthreadex(NULL, 0, ttttttt, NULL, 0, NULL); system("pause"); return 0; }
#include <Windows.h> #include <tchar.h> #include <stdio.h> #include <process.h> #include <winbase.h> // 读写锁 与条件变量 共享模式 SRWLOCK srwLock; //条件变量 CONDITION_VARIABLE condition_Var; unsigned WINAPI ReadThread1(LPVOID) { while(1) { AcquireSRWLockShared(&srwLock); printf("线程1 准备等待条件变量\n"); SleepConditionVariableSRW(&condition_Var, &srwLock, INFINITE, CONDITION_VARIABLE_LOCKMODE_SHARED); printf("线程1 等到了\n"); ReleaseSRWLockShared(&srwLock); Sleep(500); } return 0; } unsigned WINAPI ReadThread2(LPVOID) { while(1) { AcquireSRWLockShared(&srwLock); printf("线程2 准备等待条件变量\n"); SleepConditionVariableSRW(&condition_Var, &srwLock, INFINITE, CONDITION_VARIABLE_LOCKMODE_SHARED); printf("线程2 等到了\n"); ReleaseSRWLockShared(&srwLock); Sleep(500); } return 0; } unsigned WINAPI WriteThread(LPVOID) { while(1) { AcquireSRWLockExclusive(&srwLock); printf("write\n"); ReleaseSRWLockExclusive(&srwLock); Sleep(1000); } return 0; } int main() { InitializeSRWLock(&srwLock); InitializeConditionVariable(&condition_Var); CloseHandle( (HANDLE)_beginthreadex(NULL, 0, ReadThread1, NULL, 0, NULL) ); CloseHandle( (HANDLE)_beginthreadex(NULL, 0, ReadThread2, NULL, 0, NULL) ); // CloseHandle( // (HANDLE)_beginthreadex(NULL, 0, WriteThread, NULL, 0, NULL) ); // Sleep(2000); printf("准备激活条件变量\n"); //如果激活时,没有条件变量在等待,那么激活状态会丢失,就是说只有等待的时候,激活才有用 WakeAllConditionVariable(&condition_Var); Sleep(100); printf("main over\n"); system("pause"); return 0; }
下面的代码,介绍了这么使用读写锁和条件变量
#include <Windows.h> #include <tchar.h> #include <stdio.h> #include <process.h> #include <winbase.h> // 读写锁 与条件变量 共享模式 //苹果数 LONG volatile gl_apple = 0; SRWLOCK srwLock; //条件变量 CONDITION_VARIABLE CV_Eat; CONDITION_VARIABLE CV_Make; #define EATTIME 100 #define MAKETIME 200 void PrintApple() { printf("目前还剩 %d apple\n", gl_apple); } //吃苹果 unsigned WINAPI AllenEatApple(LPVOID) { while(1) { AcquireSRWLockShared(&srwLock); printf("Allen 准备吃苹果\n"); while (InterlockedCompareExchange(&gl_apple, 0, 0) <= 0) { printf("allen 还没有苹果,等等吧\n"); SleepConditionVariableSRW(&CV_Eat, &srwLock, INFINITE, CONDITION_VARIABLE_LOCKMODE_SHARED); } InterlockedDecrement(&gl_apple); printf("allen 吃完了\n"); PrintApple(); ReleaseSRWLockShared(&srwLock); WakeConditionVariable(&CV_Make); printf("allen say: make apple\n"); Sleep(EATTIME); } return 0; } unsigned WINAPI LiMeiEatApple(LPVOID) { while(1) { AcquireSRWLockShared(&srwLock); printf("LiMei 准备吃苹果\n"); while (InterlockedCompareExchange(&gl_apple, 0, 0) <= 0) { printf("LiMei 还没有苹果,等等吧\n"); SleepConditionVariableSRW(&CV_Eat, &srwLock, INFINITE, CONDITION_VARIABLE_LOCKMODE_SHARED); } InterlockedDecrement(&gl_apple); printf("LiMei 吃完了\n"); PrintApple(); ReleaseSRWLockShared(&srwLock); WakeConditionVariable(&CV_Make); printf("LiMei say: make apple\n"); Sleep(EATTIME); } return 0; } unsigned WINAPI MakeApple(LPVOID) { while(1) { AcquireSRWLockExclusive(&srwLock); //只能生产10个 if(InterlockedCompareExchange(&gl_apple,10,10) >= 10) { //独占锁 等待消费 printf("够数了,不生产了\n"); SleepConditionVariableSRW(&CV_Make, &srwLock, INFINITE, 0); } InterlockedIncrement(&gl_apple); printf("生产了一个,目前共有: %d \n",gl_apple); ReleaseSRWLockExclusive(&srwLock); WakeAllConditionVariable(&CV_Eat);//告诉他们,可以吃了 Sleep(MAKETIME); } return 0; } int main() { InitializeSRWLock(&srwLock); InitializeConditionVariable(&CV_Eat); InitializeConditionVariable(&CV_Make); CloseHandle( (HANDLE)_beginthreadex(NULL, 0, AllenEatApple, NULL, 0, NULL) ); CloseHandle( (HANDLE)_beginthreadex(NULL, 0, LiMeiEatApple, NULL, 0, NULL) ); CloseHandle( (HANDLE)_beginthreadex(NULL, 0, MakeApple, NULL, 0, NULL) ); system("pause"); return 0; }