例子
#include <iostream>
#include <thread>
#include <atomic>
#include <vector>
std::atomic<long long> data;
long long _data = 0;
void do_work()
{
//_data++;
data.fetch_add(1, std::memory_order_relaxed);
}
int main()
{
std::vector<std::thread> thread_pool;
for (int i = 0; i < 1000; i++) {
std::thread t(do_work);
thread_pool.push_back(std::move(t));
}
for (int i = 0 ; i < 1000 ; i++) {
thread_pool[i].join();
}
//std::cout << "Result:" << _data << '\n';
std::cout << "Result:" << data << '\n';
}