Monday, November 1, 2010

Slim Read-Write (SRW) Locks

Tip - A Slim Read-Write lock is a variation of the critical section object. The difference is that it permits multiple read locks at the same time.

Details - Usually we will be having a single writer thread and multiple reading threads. If we are using a critical section object for synchronization then each read thread will have to wait for the other read threads to leave the critical section. The SRW lock improves the situation by allowing multiple read locks by different threads at the same time. This will provide greater concurrency, especially on multi processors. When the writer thread wants to acquire the lock, it will wait for all the read threads to leave the SRW lock.

API - Following is the API to acquire and release a read lock.

AcquireSRWLockShared( PSRWLOCK SRWLock )
ReleaseSRWLockShared( PSRWLOCK SRWLock )

Following is the API to acquire and release a write lock.

AcquireSRWLockExclusive(PSRWLOCK SRWLock )
ReleaseSRWLockExclusive(PSRWLOCK SRWLock )

Performance - The following table shows the performance of a task that is performed in multiple threads using different synchronization mechanisms, on a dual processor system.
Threads/Milliseconds
Critical Section
SRWLock Shared
SRWLock Exclusive
Mutex
1
66
66
67
1060
2
268
134
148
1082
4
768
244
307
23785

Looking at the performance values, we can clearly see that there is a big performance gain for SRW locks when compared with a critical section object.

Limitations
There is no API like TryEnterxxx() as in the case of a critical section. A thread need to wait till it gets the requested lock. This limitation is only applicable to Windows Vista only. Windows 7 provides the TryAcquireSRWLockExclusive() and TryAcquireSRWLockShared().


The SRW locks cannot be acquired recursively. That means a thread cannot acquire multiple locks and then perform the corresponding number of releases. Currently this limitation is applicable to Windows Vista. This limitation might be removed higher versions.


Platform
Minimum requirement is Windows Vista.


References:

Posted By : Mohammed Nisamudheen S.

No comments:

Post a Comment