I rewrite function who get all combination of vector. I tried to run it asynchronously, when the vector size is 5 - getAllCombinationOfVector({ 1,2,3,4,5}) it work fine. when the vector size is 6 it stuck. When the input is 6 the code never reach to the end of any function.
#include <thread>
#include <future>
#include <iostream>
#include <chrono>
std::vector<std::vector<int>> getAllCombinationOfVector(const std::vector<int> vec)
{
return internalGetAllCombinationOfVector(vec, std::vector<int>());
}
std::vector<std::vector<int>> internalGetAllCombinationOfVector(const std::vector<int> remainIds, const std::vector<int> firstPart)
{
std::vector<std::vector<int>> options;
std::vector<int> newPart = firstPart;
if (remainIds.size() == 1)
{
newPart.push_back(remainIds[0]);
return std::vector<std::vector<int>>{newPart};
}
std::vector<std::future<std::vector<std::vector<int>>>> tasks;
for (auto itr=remainIds.begin();itr!=remainIds.end();itr++)
{
newPart = firstPart;
newPart.push_back(*itr);
std::vector<int> remainIdWithoutUsedId = std::vector<int>(remainIds.begin(),itr);
remainIdWithoutUsedId.insert(remainIdWithoutUsedId.end(),itr + 1, remainIds.end());
tasks.push_back(std::async(std::launch::async, internalGetAllCombinationOfVector, remainIdWithoutUsedId, newPart));
}
for (auto& task : tasks)
{
auto someOptions = task.get();
options.insert(options.end(), someOptions.begin(), someOptions.end());
}
return options;
}
when the function run synchronously it work. What can be the problem?
Aucun commentaire:
Enregistrer un commentaire