mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
see #2: update std::atomic and memory model
This commit is contained in:
19
code/7/7.6.atomic.cpp
Normal file
19
code/7/7.6.atomic.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <iostream>
|
||||
|
||||
std::atomic<int> count = {0};
|
||||
|
||||
int main() {
|
||||
std::thread t1([](){
|
||||
count.fetch_add(1);
|
||||
});
|
||||
std::thread t2([](){
|
||||
count++; // identical to fetch_add
|
||||
count += 1; // identical to fetch_add
|
||||
});
|
||||
t1.join();
|
||||
t2.join();
|
||||
std::cout << count << std::endl;
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user