Slim Reader/Writer (SRW) Locks

Slim reader/writer (SRW) locks enable the threads of a single process to access shared resources; they are optimized for speed and occupy very little memory.

Reader threads read data from a shared resource whereas writer threads write data to a shared resource. When multiple threads are reading and writing using a shared resource, exclusive locks such as a critical section or mutex can become a bottleneck if the reader threads run continuously but write operations are rare.

SRW locks provide two modes in which threads can access a shared resource:

  • Shared mode grants shared read-only access to multiple reader threads, which enables them to read data from the shared resource concurrently. If read operations exceed write operations, this concurrency increases performance and throughput compared to critical sections.
  • Exclusive mode grants read/write access to one writer thread at a time. When the lock has been acquired in exclusive mode, no other thread can access the shared resource until the writer releases the lock.

A single SRW lock can be acquired in either mode; reader threads can acquire it in shared mode whereas writer threads can acquire it in exclusive mode. There is no guarantee about the order in which threads that request ownership will be granted ownership; SRW locks are neither fair nor FIFO.

An SRW lock is the size of a pointer. The advantage is that it is fast to update the lock state. The disadvantage is that very little state information can be stored, so SRW locks cannot be acquired recursively. In addition, a thread that owns an SRW lock in shared mode cannot upgrade its ownership of the lock to exclusive mode.

The following are the SRW lock functions.

SRW lock functionDescription
AcquireSRWLockExclusiveAcquires an SRW lock in exclusive mode.
AcquireSRWLockSharedAcquires an SRW lock in shared mode.
InitializeSRWLockInitialize an SRW lock.
ReleaseSRWLockExclusiveReleases an SRW lock that was opened in exclusive mode.
ReleaseSRWLockSharedReleases an SRW lock that was opened in shared mode.
SleepConditionVariableSRWSleeps on the specified condition variable and releases the specified lock as an atomic operation.

 

Send comments about this topic to Microsoft

Build date: 11/12/2009

Tags :


Community Content

Hayling
Use in .NET
This functionality can only be used with Vista/Windows 2008 or later. To use it in .NET, one can use the SlimResourceLock class from Jeffrey Richter’s Power Threading Library (http://www.wintellect.com/PowerThreading.aspx). Alternatively, take a look at the ReaderWriterLockSlim class from System.Threading.
Tags : threading

Page view tracker