0

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?

  • Can you please provide your input on which caused the program stucks? Your code runs smoothly in my environment with input `{1, 2, 3, 4, 5, 6}` and `{6, 5, 4, 3, 2, 1}` – prehistoricpenguin Apr 14 '21 at 07:21
  • 1
    please provide a [mre] – Alan Birtles Apr 14 '21 at 07:23
  • 1
    Cannot reproduce, on my system it works. Note, however, that with 6 input vector elements, the number of async calls is 1236, which is a lot of threads that needs to be handled by the system scheduler. Why do you force the `std::launch::async` policy? The main purpose of `async` is to let the runtime decide how to execute the function. – Daniel Langr Apr 14 '21 at 07:36
  • Notice there is [`std::next_permutation`](https://en.cppreference.com/w/cpp/algorithm/next_permutation) to iterate over all permutations. – Jarod42 Apr 14 '21 at 07:47
  • 1
    I assume your version of std::async is using a thread pool with a certain maximum number of threads, and all of them are waiting. – user253751 Apr 14 '21 at 08:27
  • @prehistoricpenguin I ran '''getAllCombinationOfVector({ 1,2,3,4,5,6})''' on windows with visual studio 2015. – Alon Huber Apr 14 '21 at 08:43
  • @ user253751 I suspected it can be something like that, how can prevent it from happenning? – Alon Huber Apr 14 '21 at 08:46
  • @AlonHuber What happens when you call `std::async` without the specified async policy? Oversubscription usually may make things even worse if you care about performance. – Daniel Langr Apr 14 '21 at 08:51
  • @ Daniel Langr I didn't check the performence yet. but when I remove the specified async policy it's behave like it behave with specified async policy(stuck). – Alon Huber Apr 14 '21 at 09:39

0 Answers0