Using the ReaderWriterLock class to implement multi-user read/single-user write synchronization [1]
2015-03-12
Applications have read operations while accessing resources, with relatively few write operations. To address this problem, C # provides a System.Threading.ReaderWriterLock class to accommodate multi-user read/single-user write scenarios.
This class implements the following functions: If a resource is not locked by a write operation, then any thread can lock the resource for read operations, and there is no limit to the number of read operation locks, that is, multiple threads can lock the resource at the same time to read the data. If a resource is not added to any read or write lock, then one and only one thread can add write locks to the resource to write to the data. The simple thing to say is that a read operation lock is a shared lock that allows multiple threads to read data simultaneously; a write operation Lock is an exclusive lock, and at the same time, only one thread is allowed to write.
The code is as follows:
1 usingSystem;2 usingSystem.Threading;3 4 namespaceprocesstest5 {6 class Program7 { 8 //read, write operation Lock9 StaticReaderWriterLock RWL =NewReaderWriterLock ();Ten One Static voidMain (string[] args) A { - //Create 2 read operations threads, 2 write threads, and start a -Thread TR0 =NewThread (NewParameterizedthreadstart (Read)); theThread TR1 =NewThread (NewParameterizedthreadstart (Read)); -Thread TR2 =NewThread (NewParameterizedthreadstart (Write)); -Thread TR3 =NewThread (NewParameterizedthreadstart (Write)); - +Tr0. Start ("R0"); -TR1. Start ("R1"); +TR2. Start ("W2"); ATR3. Start ("W3"); at - //wait for the thread to finish executing - tr0. Join (); - TR1. Join (); - TR2. Join (); - TR3. Join (); in - System.Console.ReadKey (); to } + - //Read Data the Static voidRead (ObjectState ) * { $ for(inti =0; I <3; i++)Panax Notoginseng { - Try the { + //Request read Operation Lock, if the read operation lock is not acquired within 1000ms, discard ARwl. AcquireReaderLock ( +); theConsole.WriteLine ("Begin Read,thread = {0}", state); +Thread.Sleep (Ten); -Console.WriteLine ("End Read,thread = {0}", state); $ //release read operation Lock $ RWL. Releasereaderlock (); - } - Catch(ApplicationException) the { - //get processing of read lock failureWuyi } the } - } Wu - //Write Data About Static voidWrite (ObjectState ) $ { - for(inti =0; I <3; i++) - { - Try A { + //Write operation Lock is requested, if the write lock is not acquired within 1000ms, discard theRwl. AcquireWriterLock ( +); -Console.WriteLine ("Begin Write,thread = {0}", state); $Thread.Sleep ( -); theConsole.WriteLine ("End Write,thread = {0}", state); the //Release the write operation Lock the RWL. Releasewriterlock (); the } - Catch(ApplicationException) in { the //failed to get write operation Lock the } About } the } the } the}
The results are as follows:
Reference:
[1] C # multithreaded programming: Using the ReaderWriterLock class for multi-user read/single-user write synchronization
[2] Summary: Several methods of C # thread synchronization
Using the ReaderWriterLock class to implement multi-user read/single-user write synchronization