SDL3pp
A slim C++ wrapper for SDL3
|
A mutex that allows read-only threads to run in parallel. More...
Public Member Functions | |
constexpr | RWLock (const RWLockRaw resource) |
Constructs from RWLockParam. More... | |
constexpr | RWLock (const RWLock &other)=delete |
Copy constructor. | |
constexpr | RWLock (RWLock &&other) |
Move constructor. | |
constexpr | RWLock (const RWLockRef &other)=delete |
constexpr | RWLock (RWLockRef &&other)=delete |
RWLock () | |
Create a new read/write lock. More... | |
~RWLock () | |
Destructor. | |
RWLock & | operator= (RWLock other) |
Assignment operator. | |
constexpr RWLockRaw | get () const |
Retrieves underlying RWLockRaw. | |
constexpr RWLockRaw | release () |
Retrieves underlying RWLockRaw and clear this. | |
constexpr auto | operator<=> (const RWLock &other) const =default |
Comparison. | |
constexpr bool | operator== (std::nullptr_t _) const |
Comparison. | |
constexpr | operator bool () const |
Converts to bool. | |
constexpr | operator RWLockParam () const |
Converts to RWLockParam. | |
void | Destroy () |
Destroy a read/write lock created with RWLock.RWLock(). More... | |
void | LockForReading () |
Lock the read/write lock for read only operations. More... | |
void | LockForWriting () |
Lock the read/write lock for write operations. More... | |
void | TryLockForReading () |
Try to lock a read/write lock for reading without blocking. More... | |
void | TryLockForWriting () |
Try to lock a read/write lock for writing without blocking. More... | |
void | Unlock () |
Unlock the read/write lock. More... | |
A rwlock is roughly the same concept as Mutex, but allows threads that request read-only access to all hold the lock at the same time. If a thread requests write access, it will block until all read-only threads have released the lock, and no one else can hold the thread (for reading or writing) at the same time as the writing thread.
This can be more efficient in cases where several threads need to access data frequently, but changes to that data are rare.
There are other rules that apply to rwlocks that don't apply to mutexes, about how threads are scheduled and when they can be recursively locked. These are documented in the other rwlock functions.
|
inlineexplicitconstexpr |
resource | a RWLockRaw to be wrapped. |
This assumes the ownership, call release() if you need to take back.
|
inline |
A read/write lock is useful for situations where you have multiple threads trying to access a resource that is rarely updated. All threads requesting a read-only lock will be allowed to run in parallel; if a thread requests a write lock, it will be provided exclusive access. This makes it safe for multiple threads to use a resource at the same time if they promise not to change it, and when it has to be changed, the rwlock will serve as a gateway to make sure those changes can be made safely.
In the right situation, a rwlock can be more efficient than a mutex, which only lets a single thread proceed at a time, even if it won't be modifying the data.
All newly-created read/write locks begin in the unlocked state.
Calls to RWLock.LockForReading() and RWLock.LockForWriting will not return while the rwlock is locked for writing by another thread. See RWLock.TryLockForReading() and RWLock.TryLockForWriting() to attempt to lock without blocking.
SDL read/write locks are only recursive for read-only locks! They are not guaranteed to be fair, or provide access in a FIFO manner! They are not guaranteed to favor writers. You may not lock a rwlock for both read-only and write access at the same time from the same thread (so you can't promote your read-only lock to a write lock without unlocking first).