see #2: add atomic

This commit is contained in:
Changkun Ou
2019-07-16 18:33:43 +02:00
parent 5aff35b0f7
commit 5e9c33002a
2 changed files with 53 additions and 1 deletions

View File

@@ -0,0 +1,23 @@
#include <thread>
#include <iostream>
int main() {
int a = 0;
volatile int flag = 0;
std::thread t1([&]() {
while (flag != 1);
int b = a;
std::cout << "b = " << b << std::endl;
});
std::thread t2([&]() {
a = 5;
flag = 1;
});
t1.join();
t2.join();
return 0;
}