Created
February 23, 2014 22:06
-
-
Save noorus/9178029 to your computer and use it in GitHub Desktop.
Scoped SRWLOCK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! \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