读写锁 SRWLOCK
读写锁在对资源进行保护的同时,还能区分想要读取资源值的线程(读取者线程)和想要更新资源的线程(写入者线程)。
对于读取者线程,读写锁会允许他们并发的执行。当有写入者线程在占有资源时,读写锁会让其它写入者线程和读取者线程等待。
用读写锁来解决读者写者问题会使代码非常清晰和简洁。
typedef RTL_SRWLOCK SRWLOCK, *PSRWLOCK; typedef struct _RTL_SRWLOCK { PVOID Ptr; } RTL_SRWLOCK, *PRTL_SRWLOCK; // 初始化读写锁 WINBASEAPI VOID WINAPI InitializeSRWLock ( __out PSRWLOCK SRWLock ); // 独占式访问 WINBASEAPI VOID WINAPI AcquireSRWLockExclusive ( __inout PSRWLOCK SRWLock ); // 共享式访问 WINBASEAPI VOID WINAPI AcquireSRWLockShared ( __inout PSRWLOCK SRWLock ); // 独占式释放 WINBASEAPI VOID WINAPI ReleaseSRWLockExclusive ( __inout PSRWLOCK SRWLock ); // 共享式释放 WINBASEAPI VOID WINAPI ReleaseSRWLockShared ( __inout PSRWLOCK SRWLock );
// SRWLock.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <windows.h> #include <process.h> using namespace std; unsigned int __stdcall ReadThread(PVOID ParameterData); unsigned int __stdcall WriteThread(PVOID ParameterData); const int NUM = 30, READER_SIZE = 10; HANDLE ThreadRead[NUM], ThreadWrite[NUM]; SRWLOCK __SRWLock; int main() { InitializeSRWLock(&__SRWLock); int i = 0; for (; i<NUM; i++) { ThreadRead[i] = (HANDLE)_beginthreadex(NULL, 0, ReadThread, NULL, 0, NULL); ThreadWrite[i] = (HANDLE)_beginthreadex(NULL, 0, WriteThread, NULL, 0, NULL); } WaitForMultipleObjects(NUM, ThreadRead, TRUE, INFINITE); WaitForMultipleObjects(NUM, ThreadWrite, TRUE, INFINITE); cout << "运行完毕" << endl; getchar(); return 0; } unsigned int __stdcall ReadThread(PVOID ParameterData) { AcquireSRWLockShared(&__SRWLock); cout << "ID" << GetCurrentThreadId() << " Read Thread Begin!" << endl; Sleep(100); cout << "ID" << GetCurrentThreadId() << " Read Thread Terminate!" << endl; ReleaseSRWLockShared(&__SRWLock); return 0; } unsigned int __stdcall WriteThread(PVOID ParameterData) { AcquireSRWLockExclusive(&__SRWLock); cout << "ID" << GetCurrentThreadId() << " WRITE Thread Begin~~~~~~~~~~" << endl; Sleep(200); cout << "ID" << GetCurrentThreadId() << " WRITE Thread Terminate~~~~~~" << endl; ReleaseSRWLockExclusive(&__SRWLock); return 0; }