Jac*_*son 7 c++ concurrency multithreading c++11
我正在开发一个程序,我想在循环中使用异步.在示例代码中,我只包含了10个元素,因此我可以轻松地为每个元素创建一个显式变量.但是,在我的主程序中,向量中的元素数量可能会有所不同.理想情况下,我想创建一个异步线程的向量 - 一个用于数组中的每个元素 - 当我循环时,它们被推回到异步向量上.然后我想等待它们全部完成,然后使用" get()"返回所有输出.
下面的代码将通过为每个线程分配一个显式变量来调用async,但有没有人知道如何在向量中动态调用async而不必为其明确赋值变量?理想情况下,我希望这个程序在每次循环时调用"std :: cout"一次,而不是只调用一次.
#include <iostream>
#include <vector>
#include <string>
#include <future>
std::string hi (std::string input)
{
return "hello, this is " + input;
}
int main()
{
std::vector<std::string> test_vector( 10, "a test" );
std::future<std::string> a;
std::future<std::string> b;
for ( int i = 0; i < test_vector.size ( ); i++ )
{
a = std::async(std::launch::async, hi, test_vector[i]);
}
std::cout << a.get() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Gal*_*lik 14
你可以通过创建一个期货矢量来匹配你的线程向量来解决这个问题,如下所示:
#include <iostream>
#include <vector>
#include <string>
#include <future>
std::string hi(const std::string& input)
{
return "hello, this is " + input;
}
int main()
{
std::vector<std::string> tests = {"one", "two", "three", "four"};
std::vector<std::future<std::string>> futures;
// add the futures to the futures vector as you launch
// your asynchronous functions
for(auto&& t: tests)
futures.emplace_back(std::async(std::launch::async, hi, std::cref(t)));
// collect your results
for(auto&& f: futures)
std::cout << f.get() << '\n';
}
Run Code Online (Sandbox Code Playgroud)
注意使用std :: cref传递const引用.使用std :: ref传递非const引用.
答案包括std::cout
:
std::vector<std::future<std::string>> a;
for (int i = 0; i < 10; ++i) {
a.emplace_back(std::async(hi));
}
for (auto& element : a) {
std::cout << element.get() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2527 次 |
最近记录: |