参考:
std::async - cppreference.comhttps://en.cppreference.com/w/cpp/thread/async
简介:
c++11 提供了便捷的多线程运行库,比如std::thread ,std::future,std::condition_variable等等,std::async函数就是其中之一,其接受一个可执行对象,也接受一个参数入参(注意内存安全),然后独立启动一个线程执行可执行对象,或者在调用std::async的线程中执行可执行对象。如果指定std::launch::async标志,则立刻启动独立线程运行可执行对象,如果指定std::launch::deferred标志,则std::async会先创建一个std::thread,然后继续执行后面的内容,当对std::async构造返回的std::future调用的不带超时时间的wait()函数时,会在调用线程中调用可执行对象,可以认为wait()函数会变成可调用对象然后执行。
std::async构造会返回一个 std::futrue,可以通过调用 std::future的wait() 函数实现阻塞等待可执行对象执行完毕,同时也可以调用 std::future的get()函数来获取可执行对象的return 返回值(如果不return void的话)。
std::launch::async : 立刻启动线程开始运行可执行对象;
std::launch::deferred : 延迟启动可执行对象,启动时时间点为 std::future 的 wait() 时间点。
Demo:
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <future>
#include <string>
#include <mutex>
#include <thread>
std::mutex m;
struct X {
void foo(int i, const std::string& str) {
std::lock_guard<std::mutex> lk(m);
std::cout << str << ' ' << i << '\n';
}
void bar(const std::string& str) {
std::lock_guard<std::mutex> lk(m);
std::cout << str << " | std::launch::deferred thread id : " << std::this_thread::get_id() << '\n';
}
int operator()(int i) {
std::lock_guard<std::mutex> lk(m);
std::cout << i << '\n';
return i + 10;
}
};
template <typename RandomIt>
int parallel_sum(RandomIt beg, RandomIt end)
{
auto len = end - beg;
if (len < 1000)
return std::accumulate(beg, end, 0);
RandomIt mid = beg + len/2;
auto handle = std::async(std::launch::async,
parallel_sum<RandomIt>, mid, end);
int sum = parallel_sum(beg, mid);
return sum + handle.get();
}
int main()
{
std::vector<int> v(10000, 1);
std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << '\n';
std::cout << "Main thread id :" << std::this_thread::get_id() << '\n';
X x;
// Calls (&x)->foo(42, "Hello") with default policy:
// may print "Hello 42" concurrently or defer execution
auto a1 = std::async(&X::foo, &x, 42, "Hello");
// Calls x.bar("world!") with deferred policy
// prints "world!" when a2.get() or a2.wait() is called
auto a2 = std::async(std::launch::deferred, &X::bar, x, "world!");
std::cout << "world will be after this " << '\n';
a2.wait();
std::cout << "world will be before this " << '\n';
// Calls X()(43); with async policy
// prints "43" concurrently
auto a3 = std::async(std::launch::async, X(), 43);
// prints "world!"
std::cout << a3.get() << '\n'; // prints "53"
} // if a1 is not done at this point, destructor of a1 prints "Hello 42" here