An Even Further Clarification on volatile

Volatile, in C, forces the compiler to assume an operation has side effects. That means it can’t be eliminated even if the rest of the block doesn’t make use of the operation involving the volatile element.

I strongly recommend this blog post to anyone translating or writing code involving a volatile pointer, because to echo @dimdin, ordering operations is not one of the things it does.

There are some open questions about the role of the volatile keyword in Zig, as distinct from C. In terms of how it actually works right now, today, it functions in the same way.

But if you need to order operations, that’s what atomics do. Sometimes you’ll also need volatile, sometimes you won’t.

2 Likes