Lightweight read-write lock (Slim reader-writer locks): Read-write lock is actually a special spin lock, it divides the audience of the shared resources into readers and writer, readers only read access to the shared resources, the writer needs to write to the shared resources.
This lock improves concurrency relative to spin locks because it allows multiple readers to access shared resources at the same time in a multiprocessor system, with the maximum possible number of readers being the actual number of logical CPUs.
The writer is exclusive, and a read-write lock can only have one writer or multiple readers (related to the number of CPUs), but not both the reader and the writer.
Read-write locks are more adaptable than mutexes, have higher parallelism, and can have multiple threads concurrently occupying read-mode reading and writing locks, but only one thread consumes write-mode read-write locks and read-write locks in three states:
1. When the read-write lock is write-locked, all threads attempting to lock the lock will be blocked until the lock is unlocked.
2. When the read-write lock is in the locking state, all threads attempting to lock it in read mode can gain access, but threads that lock it in write mode will be blocked
3. When a read-write lock is in the lock state of reading mode, if another thread attempts to lock in write mode, the read-write lock usually blocks the subsequent read-mode lock request, which avoids the long-term use of the read-mode lock, while the waiting write-mode lock request is blocked for a long time.
SRW representative-------Slim Reader-writer
Function parsing:
1.//initializing a read-write lock
VOID WINAPI Initializesrwlock
(
__out Psrwlock SRWLock
);
function function: Initialize read-write lock
2.//Exclusive access
VOID WINAPI acquiresrwlockexclusive
(
__inout Psrwlock SRWLock
);
function function: Writer thread request write resource.
3.//Exclusive-Release
VOID WINAPI releasesrwlockexclusive
(
__inout Psrwlock SRWLock
);
function function: The writer thread writes the resource complete and frees up the resource usage.
4.//Shared-Access
VOID WINAPI acquiresrwlockshared
(
__inout Psrwlock SRWLock
);
function function: Reader thread request read resource.
5.//shared-Release
VOID WINAPI releasesrwlockshared
(
__inout Psrwlock SRWLock
);
function function: The reader thread ends reading the resource and frees up the resource.
Note the point:
1. The access method can only be selected, and cannot be used at the same time. That is, a thread can only lock resources once and cannot lock resources multiple times.
2. Read-write locks are initialized after they are declared, but not destroyed, and the system automatically cleans up read and write locks.
3. The reader and writer call different application functions and release functions, respectively.
Difference:
1.AcquireSRWLockExclusive preemptive, when a thread is locked, other threads cannot access it. Until Releasesrwlockexclusive, after release. Generally used for write operations.
2.AcquireSRWLockShared is shared, and other threads can access it again even after the thread is locked. Generally used for read operations.
Source code://Semaphore.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<Windows.h>#include<process.h>//Number of Threads#defineG_nthreadnum 3//Signal VolumeSRWLOCK G_srwlock;//Cumulative NumberintG_ncount = -; unsigned _stdcall writethread (void*LParam) { //Exclusive AccessAcquiresrwlockexclusive (&G_srwlock); G_ncount++; //Exclusive-ReleaseReleasesrwlockexclusive (&G_srwlock); return 0;} unsigned _stdcall readthread (void*LParam) { //Shared-AccessAcquiresrwlockshared (&G_srwlock); printf ("g_ncount=%d\n", G_ncount); //shared-ReleaseReleasesrwlockshared (&G_srwlock); return 0;}int_tmain (intARGC, _tchar*argv[]) { ////Initialize read/write lockInitializesrwlock (&G_srwlock); //Start ThreadHANDLE Pthread[g_nthreadnum]; pthread[0] = (HANDLE) _beginthreadex (NULL,0, Readthread, NULL,0,0); pthread[1] = (HANDLE) _beginthreadex (NULL,0, Writethread, NULL,0,0); pthread[2] = (HANDLE) _beginthreadex (NULL,0, Readthread, NULL,0,0); //wait for thread to endwaitformultipleobjects (G_nthreadnum, PThread, TRUE, INFINITE); printf ("g_ncount:%d\n", G_ncount); //Freeing Resources for(inti =0; i < G_nthreadnum; ++i) {CloseHandle (pthread[i]); } getchar (); return 0;}
Read-write lock for thread synchronization (supplemental to lock operation)