Another new feature of Windows Vista is the SRW-Lock. It has some advantages compared to a critical section or the TMultiReadExclusiveWriteSynchronizer of Delphi. Lets think about it.

The SRWLock was made for the case of a shared resource which will be changed from one ore more threads while there are other threads which want to read the resource.
Of course, this can be done with 1 or 2 critical sections. But 1 critical section will block the writing threads and 2 critical sections are 1 too much.

Optimized for speed and occupying very little memory the SRWLock provides us the basic features of the Delphi-known TMultiReadExclusiveWriteSynchronizer with an unbelievable performance (in my tests the SRWLock was at least 10 times faster then TMultiReadExclusiveWriteSynchronizer).
But, as my father likes to say “Fast and good is never together”, there are also caveats with the SRWLock: You can’t elevate a SharedLock to an ExclusiveLock and the lock can’t be acquired recursively.

Using the SRWLock is quite simple:

1. create a SRWLock

uses jwaWindows;

var
  MyLock : SRWLock;
begin
  InitializeSRWLock(MyLock);
end

2. the reading thread

AcquireSRWLockShared(MyLock);
//do some reading work
ReleaseSRWLockShared(MyLock) ;

3. the writing thread

AcquireSRWLockExclusive(MyLock);
//do some writing work
ReleaseSRWLockExclusive(MyLock);

Don’t try to free the SRWLock … it’s done automatically.

The JediAPI-SVN-trunk contains the needed units (jwaWindows or jwaWinBase). Just try it.