4 ms·
Volatile, at least in C and C++, is already broken when used for thread coordination. Lock-free algorithms need to use atomic instructions, which with work.
by ori_b 6y ago
Volatile, at least in C and C++, is already broken when used for thread coordination. Lock-free algorithms need to use atomic instructions, which with work.
- pantalaimon 6y agovolatile was never meant for thread coordination.
- masklinn 6y agoWhich is obvious when you consider that C didn't get any sort of thread-awareness until C11.
- ori_b 6y agoAnd, yet, people still try to use it for that. So it's worth pointing out that it doesn't work. I think volatile in general is pretty useless, and it'd be better to replace it with atomic style functions: x = volatile_read(&v); y = use(x); volatile_write(&y); Which is both clearer about what's going on, and doesn't cause optimization barriers to happen everywhere, just around the specific loads and stores you care about.
- SAI_Peregrinus 6y agoVolatile is for memory-mapped IO. It's not particularly useful anywhere else.
- ori_b 6y agoThank you for repeating me again. Though it's not even what I want then. I think the only time I've wanted volatile as is currently specified was around setjmp/longjmp -- and I think I'd prefer that setjmp/longjmp were specified in a less error prone way. If you're paying attention to the standards community, what I'm wishing for was mostly sketched out in ISO/IEC TR 18037, section 6. (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf)
- rumanator 6y agoI don't see your point. In C and C++, volatile is just a compiler hint to disable some classes of optimizations. It was never intended to be used to sync threads because it offers no such assurances.
- ori_b 6y agoCongratulations, you saw my point. People try to use it for thread coordination. It's always been wrong.
- ncmncm 6y agoI know a FreeBSD core committer who is convinced that volatile is good for thread coordination. Yes, he is wrong.
- millstone 6y agovolatile is required (but not sufficient) for thread coordination, at least in C++03 and C99. That's why boost atomics uses volatile extensively.
- ncmncm 6y agoUsing volatile as if it simply means atomic is wrong. But that is what he insisted.
- gpderetta 6y agoYou can use volatile as part of a suite of compiler specific tricks to implement higher level load acquire/store release operations on x86 (and load consume everywere else), but portably you can't do much.
- rumanator 6y ago> volatile is good for thread coordination. He has a point, though. https://en.cppreference.com/w/cpp/language/cv https://en.cppreference.com/w/cpp/language/cv > Every access (read or write operation, member function call, etc.) made through a glvalue expression of volatile-qualified type is treated as a visible side-effect for the purposes of optimization (that is, within a single thread of execution, volatile accesses cannot be optimized out or reordered with another visible side effect that is sequenced-before or sequenced-after the volatile access. This makes volatile objects suitable for communication with a signal handler, but not with another thread of execution, see std::memory_order). Any attempt to refer to a volatile object through a non-volatile glvalue (e.g. through a reference or pointer to non-volatile type) results in undefined behavior.