Search code examples
multithreadingc++11templatesstdbind

ability to run a packaged task with std::bind function parameters in a seperate thread via template function


The question i have is the line where i indicate ERROR below gives error as std::thread constructor works out the function to be invoked and requires the parameter as needed by the function signature.

Is there any way to solve this ? if i attempt to decode the function name and argument list from packaged_task then i cant use the get_future function over packaged task and need to add my own promise/future code to handle this.

#include<iostream>
#include<string>
#include<thread>
#include<future>
#include<functional>

using namespace std;

int sampleAddFunction(const int& a, const int& b)
{
    int sum = a + b;
    cout << "sum = " << sum << endl;
    return(sum);
}

template<typename T> T asyncExecutor(std::packaged_task<T(T, T)>&& package)
{
    std::future<T> result = package.get_future();
    std::thread task_td(std::move(package)); // ERROR here as the std::thread identifies the function name from package and requires the params to be passed. How to handle this ?
    task_td.join();
    return(result.get());
}

int main(int argc, char* argv[])
{

    // Executing via directly calling through main.
    int testResult1 = sampleAddFunction(100, 200);
    cout << "testResult1 = " << testResult1 << endl;

    // Attempt to create a std::packaged_task and then run it in another thread.
    std::packaged_task<int(int,int)> task(std::bind(sampleAddFunction, 10, 20));
    std::future<int> result = task.get_future();
    std::thread t(std::move(task), 100, 200); // 100 and 200 are dummy parameters.
    t.join();
    int testResult2=result.get();
    cout << "testResult2 = " << testResult2 << endl;

    // Attempt to run this in seperate thread and get results.
    std::packaged_task<int(int,int)> task2(std::bind(sampleAddFunction, 15, 27));
    int testResult3 = asyncExecutor<int>(std::move(task2), 100, 200);
    cout << "testResult3 = " << testResult3 << endl;

}

Solution

  • This should work.

    #include<iostream>
    #include<string>
    #include<thread>
    #include<future>
    #include<functional>
    
    using namespace std;
    
    int sampleAddFunction(int a, int b)
    {
       int sum = a + b;
       cout << "sum = " << sum << endl;
       return(sum);
    }
    
    template<typename R, typename F, typename... Ts> 
    R asyncExecutor(F&& package, Ts... args)
    {
        std::future<R> result = package.get_future();
        std::thread task_td(std::move(package), args...);
        task_td.join();
        return(result.get());
    }
    
    int main(int argc, char* argv[])
    {
        std::packaged_task<int(int,int)> task2(sampleAddFunction);
        int testResult3 = asyncExecutor<int>(std::move(task2), 15, 27);
        cout << "testResult3 = " << testResult3 << endl;
    }