SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
Public Member Functions | List of all members
SDL::RWLockBase Struct Reference

A mutex that allows read-only threads to run in parallel. More...

Inheritance diagram for SDL::RWLockBase:
Inheritance graph
[legend]

Public Member Functions

 RWLockBase ()
 Create a new read/write lock.
 
void LockForReading ()
 Lock the read/write lock for read only operations.
 
void LockForWriting ()
 Lock the read/write lock for write operations.
 
void TryLockForReading ()
 Try to lock a read/write lock for reading without blocking.
 
void TryLockForWriting ()
 Try to lock a read/write lock for writing without blocking.
 
void Unlock ()
 Unlock the read/write lock.
 
constexpr Resource (T resource={})
 Constructs the underlying resource.
 
constexpr Resource (std::nullptr_t)
 Equivalent to default ctor.
 
constexpr Resource (std::nullopt_t)
 Equivalent to default ctor.
 
 Resource (const Resource &other)=delete
 
 Resource (Resource &&other)=delete
 
- Public Member Functions inherited from SDL::Resource< SDL_RWLock * >
constexpr Resource (SDL_RWLock * resource={})
 Constructs the underlying resource.
 
constexpr Resource (std::nullptr_t)
 Equivalent to default ctor.
 
constexpr Resource (std::nullopt_t)
 Equivalent to default ctor.
 
 Resource (const Resource &other)=delete
 
 Resource (Resource &&other)=delete
 
Resourceoperator= (const Resource &other)=delete
 
Resourceoperator= (Resource &&other)=delete
 
constexpr operator bool () const
 True if contains a valid resource.
 
constexpr bool operator== (const Resource &other) const=default
 Comparison.
 
constexpr bool operator== (std::nullopt_t) const
 Comparison.
 
constexpr bool operator== (std::nullptr_t) const
 Comparison.
 
constexpr SDL_RWLock * get () const
 Return contained resource;.
 
constexpr SDL_RWLock * release (SDL_RWLock * newResource={})
 Return contained resource and empties or replace value.
 
constexpr const SDL_RWLock * operator-> () const
 Access to fields.
 
constexpr SDL_RWLock * operator-> ()
 Access to fields.
 

Detailed Description

A rwlock is roughly the same concept as MutexBase, 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.

Since
This struct is available since SDL 3.2.0.
Category:
Resource
See also
RWLock
RWLockRef

Constructor & Destructor Documentation

◆ RWLockBase()

SDL::RWLockBase::RWLockBase ( )
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 RWLockBase.LockForReading() and RWLockBase.LockForWriting will not return while the rwlock is locked for writing by another thread. See RWLockBase.TryLockForReading() and RWLockBase.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).

Postcondition
the initialized and unlocked read/write lock or nullptr on failure; call GetError() for more information.
Since
This function is available since SDL 3.2.0.
See also
RWLockBase.LockForReading
RWLockBase.LockForWriting
RWLockBase.TryLockForReading
RWLockBase.TryLockForWriting
RWLockBase.Unlock

Member Function Documentation

◆ LockForReading()

void SDL::RWLockBase::LockForReading ( )
inline

This will block until the rwlock is available, which is to say it is not locked for writing by any other thread. Of all threads waiting to lock the rwlock, all may do so at the same time as long as they are requesting read-only access; if a thread wants to lock for writing, only one may do so at a time, and no other threads, read-only or not, may hold the lock at the same time.

It is legal for the owning thread to lock an already-locked rwlock for reading. It must unlock it the same number of times before it is actually made available for other threads in the system (this is known as a "recursive rwlock").

Note that locking for writing is not recursive (this is only available to read-only locks).

It is illegal to request a read-only lock from a thread that already holds the write lock. Doing so results in undefined behavior. Unlock the write lock before requesting a read-only lock. (But, of course, if you have the write lock, you don't need further locks to read in any case.)

This function does not fail; if rwlock is nullptr, it will return immediately having locked nothing. If the rwlock is valid, this function will always block until it can lock the mutex, and return with it locked.

Since
This function is available since SDL 3.2.0.
See also
RWLockBase.LockForWriting
RWLockBase.TryLockForReading
RWLockBase.Unlock

◆ LockForWriting()

void SDL::RWLockBase::LockForWriting ( )
inline

This will block until the rwlock is available, which is to say it is not locked for reading or writing by any other thread. Only one thread may hold the lock when it requests write access; all other threads, whether they also want to write or only want read-only access, must wait until the writer thread has released the lock.

It is illegal for the owning thread to lock an already-locked rwlock for writing (read-only may be locked recursively, writing can not). Doing so results in undefined behavior.

It is illegal to request a write lock from a thread that already holds a read-only lock. Doing so results in undefined behavior. Unlock the read-only lock before requesting a write lock.

This function does not fail; if rwlock is nullptr, it will return immediately having locked nothing. If the rwlock is valid, this function will always block until it can lock the mutex, and return with it locked.

Since
This function is available since SDL 3.2.0.
See also
RWLockBase.LockForReading
RWLockBase.TryLockForWriting
RWLockBase.Unlock

◆ TryLockForReading()

void SDL::RWLockBase::TryLockForReading ( )
inline

This works just like RWLockBase.LockForReading(), but if the rwlock is not available, then this function returns false immediately.

This technique is useful if you need access to a resource but don't want to wait for it, and will return to it to try again later.

Trying to lock for read-only access can succeed if other threads are holding read-only locks, as this won't prevent access.

This function returns true if passed a nullptr rwlock.

Exceptions
Erroron failure.
Since
This function is available since SDL 3.2.0.
See also
RWLockBase.LockForReading
RWLockBase.TryLockForWriting
RWLockBase.Unlock

◆ TryLockForWriting()

void SDL::RWLockBase::TryLockForWriting ( )
inline

This works just like RWLockBase.LockForWriting(), but if the rwlock is not available, then this function returns false immediately.

This technique is useful if you need exclusive access to a resource but don't want to wait for it, and will return to it to try again later.

It is illegal for the owning thread to lock an already-locked rwlock for writing (read-only may be locked recursively, writing can not). Doing so results in undefined behavior.

It is illegal to request a write lock from a thread that already holds a read-only lock. Doing so results in undefined behavior. Unlock the read-only lock before requesting a write lock.

This function returns true if passed a nullptr rwlock.

Exceptions
Erroron failure.
Since
This function is available since SDL 3.2.0.
See also
RWLockBase.LockForWriting
RWLockBase.TryLockForReading
RWLockBase.Unlock

◆ Unlock()

void SDL::RWLockBase::Unlock ( )
inline

Use this function to unlock the rwlock, whether it was locked for read-only or write operations.

It is legal for the owning thread to lock an already-locked read-only lock. It must unlock it the same number of times before it is actually made available for other threads in the system (this is known as a "recursive rwlock").

It is illegal to unlock a rwlock that has not been locked by the current thread, and doing so results in undefined behavior.

Since
This function is available since SDL 3.2.0.
See also
RWLockBase.LockForReading
RWLockBase.LockForWriting
RWLockBase.TryLockForReading
RWLockBase.TryLockForWriting

The documentation for this struct was generated from the following file: