As we all know, the implementation of thread synchronization on the Windows platform, or the locking and unlock methods of resources have kernel events, critical areas, mutexes, semaphores, and even interlocked series of functions and other means. However, in the daily programming, we use these means to "multiple threads simultaneously read and write to the same resource," before reading and writing to the resources of the false lock, read and write after the resources to unlock.
Imagine a situation in which an FTP server is frequently downloading files for the FTP service on a daily basis, but these files are not updated for almost several days. Every time we download the file, we need to lock the file to ensure that no one else writes to the file at the same time. However, these lock-up behavior, at 99%, will not be written to the file at the same time, only 1% of the cases will be written at the same time the file. In this way, we have a lot of locks on the waste, and you are in the lock while others, even if they just read files, but also need to wait until you unlock.
The solution to this problem is to set a shared lock on the read file, and multiple threads can read the file at the same time without blocking each other. The exclusive lock is then set, and when the file is written, an exclusive lock is added so that other threads cannot read or write at this time.
Windows provides a shared/exclusive lock called Slim to resolve this issue. However, Slim is only supported in Vista and Windows Server 2008. There is no support on previous versions. So, I will use the existing thread synchronization means, to simulate the slim to achieve a shared/exclusive lock function, the code is encapsulated as follows:
</pre><pre name= "code" class= "CPP" >//share and exclusive lock (read no lock, write lock), suitable for resource read frequency higher than write//shared lock: Everyone can read at the same time, but cannot write. Exclusive lock: is only one person exclusive use, whether it is read or write//rule: Acquire and release must appear in pairs, do not support nesting and mutual nesting//disadvantage: The lock process itself requires critical zone control, resulting in subtle performance loss #ifdef __ Cplusplusextern "C" {#endifstruct selock//shared & Exclusive lock{rtl_critical_section sec_shared,sec_exclusiv E The lock code itself is controlled by critical area HANDLE exclusive_evt; HANDLE shared_evt; Volatile Long shared_count;};/ /Initialize a SE lock _inline void initializeselock (Selock *lock) {initializecriticalsection (&lock->sec_shared); InitializeCriticalSection (&lock->sec_exclusive); Lock->exclusive_evt = CREATEEVENTW (null,true,true,null); Lock->shared_evt = CREATEEVENTW (null,true,true,null); Lock->shared_count = 0;} Cleans up an SE lock _inline void deleteselock (Selock *lock) {deletecriticalsection (&lock->sec_shared); DeleteCriticalSection (&lock->sec_exclusive); CloseHandle (LOCK->EXCLUSIVE_EVT); CloseHandle (LOCK->SHARED_EVT); lock->shared_count = 0;} Request a shared lock for reading _inline void acquireselockshared (Selock *lock) {entercriticalsection (&lock->sec_exclusive); EnterCriticalSection (&lock->sec_shared); WaitForSingleObject (Lock->exclusive_evt,infinite); Waiting for exclusive lock ++lock->shared_count; if (lock->shared_count) resetevent (LOCK->SHARED_EVT); Open the shared lock LeaveCriticalSection (&lock->sec_shared); LeaveCriticalSection (&lock->sec_exclusive);} Release shared lock _inline void releaseselockshared (Selock *lock) {entercriticalsection (&lock->sec_shared); --lock->shared_count; if (!lock->shared_count) SetEvent (LOCK->SHARED_EVT); Close the shared lock LeaveCriticalSection (&lock->sec_shared); }//Request exclusive lock _inline void acquireselockexclusive (Selock *lock) {entercriticalsection (&lock->sec_exclusive); WaitForSingleObject (Lock->exclusive_evt,infinite); Wait for exclusive lock WaitForSingleObject (lock->shared_evt,infinite); Waiting for a totalEnjoy lock resetevent (LOCK->EXCLUSIVE_EVT); Open Exclusive lock LeaveCriticalSection (&lock->sec_exclusive);} Release exclusive lock _inline void releaseselockexclusive (Selock *lock) {SetEvent (LOCK->EXCLUSIVE_EVT);//close exclusive lock} #ifdef __ Cplusplus} #endif