【发布时间】:2017-02-18 10:53:13
【问题描述】:
我想多次调用异步方法。一个简化的例子如下所示:
size_t counter(std::string &s)
{
return s.size();
}
void stringCountAccumulator()
{
std::vector<std::string> foos = {"this", "is", "spartaa"};
size_t total = 0;
for (std::string &s : foos)
{
std::future<size_t> fut = std::async(
std::launch::async,
counter, s);
total += fut.get();
}
std::cout << "Total: " << total;
}
看起来,fut.get() 阻止了其他未来的调用。如何在 C++ 中实现这个问题?我需要在单独的线程中调用一个函数。这个函数“返回”一个值。
【问题讨论】:
标签: c++ multithreading c++11