Based on the info of the output, can anyone explains the code below?
How can all of (a==1 && a==2 && a==3)
be true?
#include <iostream>
#include <thread>
int a = 0;
int main()
{
std::thread runThread([]() { while (true) { a = 1; a = 2; a = 3; }});
while (true)
{
if (a == 1 && a == 2 && a == 3)
{
std::cout << "Hell World!" << std::endl;
}
}
}
Output:
Hell World! Hell World! Hell World! Hell World! Hell World! Hell World! Hell World! Hell World! Hell World! Hell World! Hell World! Hell World! Hell World! Hell World! Hell World! Hell World! Hell World!
...
January 2019
I think this question is highly relevant to this link -> C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?