ReaderWriterLock.ReleaseWriterLock Method
Decrements the lock count on the writer lock.
Assembly: mscorlib (in mscorlib.dll)
| Exception | Condition |
|---|---|
| ApplicationException |
The thread does not have the writer lock. |
ReleaseWriterLock decrements the writer lock count. When the count reaches zero, the writer lock is released.
Note
|
|---|
|
If a thread has a reader lock, or no locks, calling ReleaseWriterLock throws an ApplicationException. |
The following code example shows how to acquire and release a writer lock, and how to handle the exception thrown when a request times out.
This code is part of a larger example provided for the ReaderWriterLock class.
// The complete code is located in the ReaderWriterLock // class topic. using System; using System.Threading; public class Test { // Declaring the ReaderWriterLock at the class level // makes it visible to all threads. static ReaderWriterLock rwl = new ReaderWriterLock(); // For this example, the shared resource protected by the // ReaderWriterLock is just an integer. static int resource = 0; ... // Shows how to request and release the writer lock, and // how to handle time-outs. static void WriteToResource(int timeOut) { try { rwl.AcquireWriterLock(timeOut); try { // It is safe for this thread to read or write // from the shared resource. resource = rnd.Next(500); Display("writes resource value " + resource); Interlocked.Increment(ref writes); } finally { // Ensure that the lock is released. rwl.ReleaseWriterLock(); } } catch (ApplicationException) { // The writer lock request timed out. Interlocked.Increment(ref writerTimeouts); } } ... }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note