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