Can multiple threads write the same value to the same variable at the same time safely?
问题 Can multiple threads write the same value to the same variable at the same time safely? For a specific example — is the below code guaranteed by the C++ standard to compile, run without undefined behavior and print "true", on every conforming system? #include <cstdio> #include <thread> int main() { bool x = false; std::thread one{[&]{ x = true; }}; std::thread two{[&]{ x = true; }}; one.join(); two.join(); std::printf(x ? "true" : "false"); } This is a theoretical question; I want to know