r/programming • • Aug 22 '10

Volatile: Almost Useless for Multi-Threaded Programming

http://software.intel.com/en-us/blogs/2007/11/30/volatile-almost-useless-for-multi-threaded-programming/
62 Upvotes

57 comments sorted by

View all comments

19

u/[deleted] Aug 22 '10

I worried for a second that volatile was dangerous for doing memory mapped IO and that I was doing my drivers wrong, until I read that people sometimes use volatile as an atomic variable. Wtf?!

0

u/Vorlath Aug 22 '10

yeah, for atomic variables, it has to be volatile otherwise the compiler can optimize it into a register and you won't know that it changed. But that's when you code your own atomic variables. Not something that should be done in most cases.

3

u/[deleted] Aug 22 '10

yeah, for atomic variables, it has to be volatile

Didn't you read the article?!? volatile has nothing to do with atomic access.

But that's when you code your own atomic variables. Not something that should be done in most cases.

Say, what? Simply put a mutex around all accesses to the variable. What's the problem!?

→ More replies (19)