Skip to content

Instantly share code, notes, and snippets.

@jazzpi
Created November 15, 2017 12:15
Show Gist options
  • Save jazzpi/4793ec726176a521693a631e97852f17 to your computer and use it in GitHub Desktop.
Save jazzpi/4793ec726176a521693a631e97852f17 to your computer and use it in GitHub Desktop.
Can c == c be false?
#include <iostream>
#include <thread>
// Compile with g++ -std=c++11 a.cpp -pthread
volatile int c = 0;
volatile bool done = false;
void change_c() {
while (!done) {
c++;
}
}
void check_c() {
while (c == c);
done = true;
}
int main() {
std::thread t1(change_c);
std::thread t2(check_c);
t2.join();
t1.join();
std::cout << "Done at c = " << c << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment