SDL_AtomicU32(3type)
β¨ SDL_AtomicU32 β¨
okay bestie, welcome to the SDL3 DATATYPES corner of the manual, where we're talking about SDL_AtomicU32 π it's giving "tiny struct, huge responsibility" and I'm obsessed.
πΈ NAME πΈ
SDL_AtomicU32 β a type that represents an atomic unsigned 32-bit value. that's it, that's the vibe. π
π¦ SYNOPSIS π¦
#include <SDL3/SDL_atomic.h>
typedef struct SDL_AtomicU32 { Uint32 value; } SDL_AtomicU32;
literally just one field, value, and it's a Uint32. no cap. π
π DESCRIPTION π
so here's the tea: you can use this to manage a value that stays synchronized across multiple CPUs without a race condition happening. when an app sets a value with SDL_SetAtomicU32, every other thread β no matter which CPU it's running on β will see that value when it grabs it with SDL_GetAtomicU32. CPU caches and all that? don't even worry about it, they don't get a vote. β¨
it's also super useful for atomic compare-and-swap operations: a thread can change the value as long as its current value matches what you expect it to be. and when you do that in a loop, you can guarantee data consistency across threads without a lock π«Ά
β οΈ okay but the usual warnings absolutely apply here: 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! π
and why is it a struct, you ask? because it's a struct so people don't accidentally use numeric operations on it directly. you have to use the SDL atomic functions. it's giving "protective packaging" and honestly, iconic. π
π AVAILABILITY π
this struct has been available since SDL 3.2.0. so if you're on anything older, sorry bestie π
β SEE ALSO β
go read those, they're the whole friend group fr fr π
Simple Directmedia Layer made this, it's SDL 3.4.18, and the manual page is SDL_AtomicU32(3type). stay atomic, stay slay, mwah πβ¨π§
π¦ where this page comes from
- fedora 43: SDL3-devel 3.4.18-1.fc43 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 43: SDL3-devel 3.4.18-1.fc43 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 44: SDL3-devel 3.4.18-1.fc44 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 44: SDL3-devel 3.4.18-1.fc44 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms