4 ms·
“You can not express the needed instructions in standard C or C++ code.“ As of 2011, you can. In C++ it’s with std::atomic. http://en.cppreference.com/w/cpp/at
by BenFrantzDale 8y ago
“You can not express the needed instructions in standard C or C++ code.“
As of 2011, you can. In C++ it’s with std::atomic. http://en.cppreference.com/w/cpp/atomic/atomic http://en.cppreference.com/w/cpp/atomic/atomic C11 offers comparable tools, I believe.
- burfog 8y agoNo, that is only good enough for threads. The compiler is not required to insert special instructions like "eieio" and is even free to optimize things in ways that would cause misbehavior of hardware. For example, consider two values written to the same location. The compiler is free to optimize out the first one because no valid threaded program can depend upon seeing that value briefly appear.
- BenFrantzDale 8y agoIf I understand you and the standard correctly, no: the default behavior is `memory_order_seq_cst` "On weakly-ordered systems (ARM, Itanium, PowerPC), special CPU load or memory fence instructions have to be used." http://en.cppreference.com/w/cpp/atomic/memory_order http://en.cppreference.com/w/cpp/atomic/memory_order I'm not 100% sure what you mean by your second paragraph. I think you are saying that repeated writes to a `std::atomic` can be optimized out, which sounds like it is true since `std::atomic` is not `volatile`. https://stackoverflow.com/questions/45960387/why-dont-compilers-merge-redundant-stdatomic-writes https://stackoverflow.com/questions/45960387/why-dont-compil...