When writing from different threads to memory, is there any garantuee how much data is always written atomic?
(Me wanting to avoid locks if possible).
If you don’t use atomics or locks then there are (almost) no guarantees from the compiler at all.
In fact the compiler is allowed to assume that there are no race conditions (two threads writing to the same location, or one thread writing while another reads) in your program and optimize accordingly.
However not all hope is lost, because you can use atomics and some of the operations are reasonably cheap, e.g. on x86 acquire/release loads/stores are basically free, all you lose are some possible compiler optimizations. Atomic operations themselves can be quite expensive though, but if you want a correct program, then you often cannot get around them.
For more information I’d suggest to check out the llvm atomics guide which lists all the types of atomics and their restrictions on the optimizer: LLVM Atomic Instructions and Concurrency Guide — LLVM 22.0.0git documentation
Thanks! The more I know, the less I feel I know.
I would also recommend using Zig’s builtin thread sanitizer, however I’m not sure how well it works right now, it appears to frequently be in a broken state.