I am trying to compare std::thread with std::async in this small example that I came up with for a fun exercise. I generate eight vectors with ten random numbers from 0 to 1000. I then push those vectors into a queue so that I can go ahead and multithread the sorting later. Here is the function I am using to generate the random numbers:
std::vector<int> generate(int count) { std::vector<int> newVec(count); std::random_device rd; std::mt19937 mt(rd()); std::uniform_int_distribution<> dis(0,1000); std::generate(begin(newVec),end(newVec),std::bind(dis,std::ref(mt))); return newVec; } In my thread example, I simply lock the queue, grab a vector out of it, call std::sort on it, and push that back into a list of vectors so I can merge it into one final vector in the end. That was easy enough to implement, but I am trying to figure out how to implement this same thing with std::async This is what I have tried so far:
void work(std::deque<std::vector<int>>& queue, std::list<std::vector<int>> toMerge) { std::vector<int> temp; temp = queue.front(); auto handle = std::async(std::launch::async,sortIt,temp); queue.pop_front(); toMerge.push_back(temp); } I then realized, that this would not do what I thought it would. As I do not believe this could call itself over and over again. So I tried this one:
void work(std::deque<std::vector<int>>& queue, std::list<std::vector<int>> toMerge) { if(queue.empty() { return; } else { auto handle = std::async(std::launch::async[&]{return std::sort(queue.front.begin(),queue.front.end());},work,queue,toMerge); } } But that gave me all sorts of compiler errors that I don't really know how to tackle.
How can I achieve this task?
Full code:
void sortIt(std::vector<int>& v) { std::sort(begin(v),end(v)); } void print(std::vector<int> &v) { for(auto &&e : v) { std::cout << e << " "; } std::cout << std::endl; } std::vector<int> generate(int count) { std::vector<int> newVec(count); std::random_device rd; std::mt19937 mt(rd()); std::uniform_int_distribution<> dis(0,1000); std::generate(begin(newVec),end(newVec),std::bind(dis,std::ref(mt))); return newVec; } void work(std::deque<std::vector<int>>& queue, std::list<std::vector<int>> toMerge) { //TODO: Make asnyc work } int main() { std::deque<std::vector<int>> queue; std::vector<int> tempA; std::vector<int> tempB; std::vector<int> finalVec; std::list<std::vector<int>> toMerge; for(int i = 0; i < 8; ++i) { queue.push_back(generate(10)); } work(queue,toMerge); }
asyncreturnsfuturewith results, you need to callgeton future to wait until task ends. What is your goal? Do you want to start 8 tasks by async (each task sorts one vector) and at the end merges all sorted vector into list ?std::mergeon my list of vectors usingtempA,tempB, andfinalVec.