var rwLock = new ReaderWriterLockSlim(); rwLock.EnterWriteLock(); // perform write operation rwLock.ExitWriteLock();
public class SharedResource { private ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(); public void DoWriteOperation() { try { _lock.EnterWriteLock(); // perform write operation } finally { _lock.ExitWriteLock(); } } }In this example, a shared resource class is defined, and the DoWriteOperation method uses a try-finally block to ensure that the write lock is released even if an exception occurs during the write operation. Package/Library: System.Threading.