Skip to content

Instantly share code, notes, and snippets.

@noorus
Created February 23, 2014 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noorus/9178029 to your computer and use it in GitHub Desktop.
Save noorus/9178029 to your computer and use it in GitHub Desktop.
Scoped SRWLOCK
//! \class ScopedSRWLock
//! Automation for scoped acquisition and release of an SRWLOCK.
//! \warning Lock must be initialized in advance!
class ScopedSRWLock: boost::noncopyable
{
protected:
PSRWLOCK mLock;
bool mExclusive;
public:
ScopedSRWLock( PSRWLOCK lock, bool exclusive = true ):
mLock( lock ), mExclusive( exclusive )
{
mExclusive ? AcquireSRWLockExclusive( mLock ) : AcquireSRWLockShared( mLock );
}
void unlock()
{
if ( mLock )
mExclusive ? ReleaseSRWLockExclusive( mLock ) : ReleaseSRWLockShared( mLock );
mLock = nullptr;
}
~ScopedSRWLock()
{
unlock();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment