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

A type representing an atomic integer value. More...

Public Member Functions

constexpr AtomicInt (int value=0)
 Constructor.
 
 AtomicInt (const AtomicInt &value)=delete
 
AtomicIntoperator= (const AtomicInt &value)=delete
 
constexpr operator SDL_AtomicInt * ()
 Convert to underlying type.
 
bool CompareAndSwap (int oldval, int newval)
 Set an atomic variable to a new value if it is currently an old value.
 
int Set (int v)
 Set an atomic variable to a value.
 
int Get ()
 Get the value of an atomic variable.
 
int Add (int v)
 Add to an atomic variable.
 
bool AtomicIncRef ()
 Increment an atomic variable used as a reference count.
 
bool AtomicDecRef ()
 Decrement an atomic variable used as a reference count.
 

Detailed Description

This can be used to manage a value that is synchronized across multiple CPUs without a race condition; when an app sets a value with AtomicInt.Set all other threads, regardless of the CPU it is running on, will see that value when retrieved with AtomicInt.Get, regardless of CPU caches, etc.

This is also useful for atomic compare-and-swap operations: a thread can change the value as long as its current value matches expectations. When done in a loop, one can guarantee data consistency across threads without a lock (but the usual warnings apply: if you don't know what you're doing, or you don't do it carefully, you can confidently cause any number of disasters with this, so in most cases, you should use a mutex instead of this!).

This is a struct so people don't accidentally use numeric operations on it directly. You have to use SDL atomic functions.

Since
This struct is available since SDL 3.2.0.
See also
AtomicInt.CompareAndSwap
AtomicInt.Get
AtomicInt.Set
AtomicInt.Add

Member Function Documentation

◆ Add()

int SDL::AtomicInt::Add ( int  v)
inline

This function also acts as a full memory barrier.

Note: If you don't know what this function is for, you shouldn't use it!

Parameters
vthe desired value to add.
Returns
the previous value of the atomic variable.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AtomicInt.AtomicDecRef
AtomicInt.AtomicIncRef

◆ AtomicDecRef()

bool SDL::AtomicInt::AtomicDecRef ( )
inline

Note: If you don't know what this macro is for, you shouldn't use it!

Returns
true if the variable reached zero after decrementing, false otherwise.
Thread safety:
It is safe to call this macro from any thread.
Since
This macro is available since SDL 3.2.0.
See also
AtomicInt.AtomicIncRef

◆ AtomicIncRef()

bool SDL::AtomicInt::AtomicIncRef ( )
inline

Note: If you don't know what this macro is for, you shouldn't use it!

Returns
the previous value of the atomic variable.
Thread safety:
It is safe to call this macro from any thread.
Since
This macro is available since SDL 3.2.0.
See also
AtomicInt.AtomicDecRef

◆ CompareAndSwap()

bool SDL::AtomicInt::CompareAndSwap ( int  oldval,
int  newval 
)
inline

Note: If you don't know what this function is for, you shouldn't use it!

Parameters
oldvalthe old value.
newvalthe new value.
Returns
true if the atomic variable was set, false otherwise.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AtomicInt.Get
AtomicInt.Set

◆ Get()

int SDL::AtomicInt::Get ( )
inline

Note: If you don't know what this function is for, you shouldn't use it!

Returns
the current value of an atomic variable.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AtomicInt.Set

◆ Set()

int SDL::AtomicInt::Set ( int  v)
inline

This function also acts as a full memory barrier.

Note: If you don't know what this function is for, you shouldn't use it!

Parameters
vthe desired value.
Returns
the previous value of the atomic variable.
Thread safety:
It is safe to call this function from any thread.
Since
This function is available since SDL 3.2.0.
See also
AtomicInt.Get

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