TryAcquireSRWLockExclusive function
Applies to: desktop apps | Metro style apps
Attempts to acquire a slim reader/writer (SRW) lock in exclusive mode. If the call is successful, the calling thread takes ownership of the lock.
Syntax
BOOLEAN TryAcquireSRWLockExclusive( __inout PSRWLOCK SRWLock );
Parameters
- SRWLock [in, out]
-
A pointer to the SRW lock.
Return value
If the lock is successfully acquired or the current thread already owns the lock, the return value is nonzero.
If another thread already owns the lock, the return value is zero.
Requirements
|
Minimum supported client | Windows 7 |
|---|---|
|
Minimum supported server | Windows Server 2008 R2 |
|
Header |
|
|
Library |
|
|
DLL |
|
See also
Send comments about this topic to Microsoft
Build date: 3/7/2012
What's the difference between TryAcquireSRWLockExclusive and AcquireSRWLockExclusive?
TryAcquireSRWLockExclusive returns immediately. How about AcquireSRWLockExclusive? If the lock is already owned, will it wait forever until the lock is released?
- 4/20/2012
- albert_001
Incorrect description
The part of the sentence stating "or the current thread already owns the lock" isn't correct. (at least for Windows 7)
If a lock is already owned, this function will always return zero whether a lock is owned by current thread or not.
SRWLOCK testLock;
::InitializeSRWLock( &testLock );
::AcquireSRWLockExclusive( &testLock );
if( ::TryAcquireSRWLockExclusive( &testLock ) )
{
printf( "TryTryAcquireSRWLockExclusive succeeded\n\n" );
::ReleaseSRWLockExclusive( &testLock );
}
::ReleaseSRWLockExclusive( &testLock );