SDL_Atomic

hello,
What is SDL_Atomic for, I know the atomic instructions with the mnemonic of PowerPC… :yum:

thanks.

is this a question about atomic programming in general, or SDL_Atomic specifically?

I don’t know about these PowerPC mnemonics :frowning:

C11 introduces _Atomic modifier, recently Microsoft added support for it: C11 Atomics in Visual Studio 2022 version 17.5 Preview 2 - C++ Team Blog

I have written one atomic code so far: aqueduct1/main.c at master · bottle2/aqueduct1 · GitHub

the advice I always see online is that atomic programming is hard, and so you should avoid it and try to solve most problems using classic stuff like mutexes and semaphores. people advice that you should use existing well-tested lockfree data structures rather than trying to roll out your own.

in the specific code I linked I have two uses of _Atomic. I watch for file changes, but it is common to receive a burst of several changes at once, so I use _Atomic to cancel previous tasks and always know the latest task. ugly code, but it is working so far

this website is great: Articles on computer science and optimization.

in summary; SDL_Atomic why do it? :face_in_clouds:

It’s for multithreaded programming, when you need to make sure writes and/or reads for a given piece of data complete before another thread is able to mess with them, so that the other thread doesn’t, say, get half old and half new data.

Atomics also make sure that changes are visible to other CPU cores; imagine if one core had written some new data, but that data was only in its cache and hadn’t been written out to main memory yet, and then another core tried to read it. The other core could potentially read outdated data.

Another benefit to atomics is that they can be used as a memory barrier.

3 Likes

thank you very much :+1: :hugs: