6.std::thread & std::async
线程 std::thread std::async std::future
std::thread
void f(int n);
std::thread t(f, n + 1);
t.join();void foo(bool clause) { /* do something... */ }
std::vector<std::thread> threadsVector;
threadsVector.emplace_back([]() {
// Lambda function that will be invoked
});
threadsVector.emplace_back(foo, true); // thread will run foo(true)
for (auto& thread : threadsVector) {
thread.join(); // Wait for threads to finish
}std::async
std::future
std::promise
std::packaged_task
最后更新于