mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 04:34:40 +03:00
21 lines
425 B
C++
21 lines
425 B
C++
#include <atomic>
|
|
#include <thread>
|
|
#include <vector>
|
|
#include <iostream>
|
|
|
|
std::atomic<int> counter = {0};
|
|
|
|
int main() {
|
|
std::vector<std::thread> vt;
|
|
for (int i = 0; i < 100; ++i) {
|
|
vt.emplace_back([](){
|
|
counter.fetch_add(1, std::memory_order_relaxed);
|
|
});
|
|
}
|
|
|
|
for (auto& t : vt) {
|
|
t.join();
|
|
}
|
|
std::cout << "current counter:" << counter << std::endl;
|
|
return 0;
|
|
} |