Variable number of asynchronous threads with C ++ 11

I am working on a program in which I would like to use async in a loop. In the sample code, I included only 10 elements there, so I could easily create an explicit variable for each element. However, in my main program, the number of elements in a vector can vary. Ideally, I would like to create a vector of asynchronous threads - one for each element in the array, which is discarded back to the async vector when I go through it. Then I want to wait for them to complete and then use "get ()" to return all my outputs.

In the code below, async will be called, assigning an explicit variable for each thread, but does anyone know how to dynamically call async in a vector without having to explicitly assign a variable to it? Ideally, I would like this program to call "std :: cout" once for each pass, and not just once.

#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; } 
+7
c ++ multithreading concurrency c ++ 11
source share
3 answers

Answer including 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; } 
+7
source share

You can solve this by creating a futures vector that matches your thread vector, something like this:

 #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'; } 

Note the use of std :: cref to pass by reference to const. Use std :: ref to pass non-trailing links.

+12
source share

If I understood correctly, it could be something like:

 std::vector<std::future<std::string>> vessel; for ( int i = 0; i < test_vector.size ( ); i++ ) { std::future<std::string> aux; aux = std::async(std::launch::async, hi); vessel.push_back(aux); } 

Sorry, I can’t post a comment, but this method, depending on the logic, and if it works, then you should be able to dynamically manipulate the vessel vector.


Update

even better:

 vessel.push_back(new std::future<std::string>); vessel[vessel.size()-1] = std::async(std::launch::async, hi); 

this way you do not need to explicitly declare a variable. But after that you will have to delete :

 for(int i=0; i<(int) vessel.size(); i++) { delete vessel[i]; } 
+3
source share

All Articles