Introduction
I simply need to implement FTryReadScopeLock and FTryWriteScopeLock for some of my features these days.
For the moment, the only way to try get lock is to use FScopeTryLock, which is based on a critical section.
But unfortunately, there is no try get lock method for Readers-writer lock in UE4.
Thus I’ve extend the FRWLock implementation for both Windows and pthreads.
Implementation
Extending ScopeRWLock.h
Add a FTryReadScopeLock class like this:
1 |
|
Implementation For PThreadRWLock Platform
Add TryReadLock and TryWriteLock method to PThreadRWLock.h:
1 | bool TryReadLock() |
This shall work for most of non-Windows platform.
Implementation For WindowsPlatform
Modification For MinimalWindowsApi.h
1 | MINIMAL_WINDOWS_API bool WINAPI TryAcquireSRWLockExclusive(PSRWLOCK SRWLock); |
Modification For MinimalWindowsApi.cpp
1 | MINIMAL_WINDOWS_API bool WINAPI TryAcquireSRWLockExclusive(PSRWLOCK SRWLock) |
Modification For WindowsCriticalSection.h
1 | FORCEINLINE bool TryReadLock() |
And everything is good to go.