mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 04:34:40 +03:00
see #2: add atomic
This commit is contained in:
23
code/7/7.6.atomic.pro.con.cpp
Normal file
23
code/7/7.6.atomic.pro.con.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user