1 //Slim Read/write locks for thread synchronization2 The purpose of the SRWlock is the same as the critical segment: Protect the same resource from access by other threads. 3 However, unlike the critical segment, Srwlock allows us to differentiate which thread (the reader thread) that wants to read the resource4 and which threads that want to update resource values (writer threads). Having all reader resources access the shared resource at the same time should be5 feasible, because there is no risk of destroying the data simply by reading the resource. Synchronization is required only if the writer thread wants to update the resource. 6 in this case, the writer thread should have exclusive access to the resource: no threads, either read or writer threads, are allowed access to the resource. 7 This is where Srwlock is powerful. 8 9 //steps to use:Ten //(1) The critical_section structure must be defined first One SRWLOCK G_cs; A //(2) initialize critical segment SRWLOCK -Initializesrwlock (&g_cs); - //3) Writer thread the DWORD WINAPI threadfunone (PVOID pvparam) - { -Acquiresrwlockexclusive (&g_cs); - + //write resources should be placed between the acquiresrwlockexclusive and releasesrwlockexclusive functions -G_x + + ; + AReleasesrwlockexclusive (&g_cs); at return 0; - } - //4) Reader thread - DWORD WINAPI threadfuntwo (PVOID pvparam) - { -Acquiresrwlockshared (&g_cs); in - //read resources should be placed between the acquiresrwlockshared and releasesrwlockshared functions tocout<<"Threadfuntwo:"<<g_x<<Endl; + -Releasesrwlockshared (&g_cs); the return 0; * } $ Panax Notoginseng there is no function to delete or destroy SRWLOCK because the system will automatically perform cleanup work - the The SRWlock lacks two properties compared to the critical segment. + 1) does not exist TryEnter (shared/Exclusive) Functions such as Srwlock: If the lock is already occupied, AThen call Acquiresrwlock (shared/Exclusive) blocks the calling thread. the 2) cannot get SRWLOCK recursively-that is, a thread cannot write resources multiple times and lock resources multiple times . +Then call releasesrwlock* multiple timesto release the resource lock. - $ $ - - the#include"windows.h" -#include"iostream"Wuyi using namespacestd; the LongG_x =0 ; - //(1) The critical_section structure must be defined first Wu SRWLOCK G_cs; - About //defining thread Functions 1 $ DWORD WINAPI threadfunone (PVOID pvparam); - - //Defining thread Functions 2 - DWORD WINAPI threadfuntwo (PVOID pvparam); A + intMain () the { - $ //(2) initialize critical segment SRWLOCK theInitializesrwlock (&g_cs); the the //Create thread 1 theHANDLE Hthreadone = CreateThread (NULL,0, Threadfunone,0,0, NULL); - CloseHandle (hthreadone); in the //Create thread 2 theHANDLE hthreadtwo = CreateThread (NULL,0, Threadfuntwo,0,0, NULL); About CloseHandle (hthreadtwo); the the //let the main thread hang first to make sure that the other threads perform the completion theSleep ( +); +cout<<g_x<<Endl; - the //(4) to clean up the critical_section structure, you must ensure that no resources are available to use this critical segment, otherwise unpredictable results will occurBayi the return 0 ; the } - - DWORD WINAPI threadfunone (PVOID pvparam) the { theAcquiresrwlockexclusive (&g_cs); the the //write resources should be placed between the acquiresrwlockexclusive and releasesrwlockexclusive functions -G_x + + ; the theReleasesrwlockexclusive (&g_cs); the return 0;94 } the the DWORD WINAPI threadfuntwo (PVOID pvparam) the {98Acquiresrwlockshared (&g_cs); About - //read resources should be placed between the acquiresrwlockshared and releasesrwlockshared functions101cout<<"Threadfuntwo:"<<g_x<<Endl;102 103Releasesrwlockshared (&g_cs);104 return 0; the }106