能够通过模板函数在单独的线程中 运行 具有 std::bind 函数参数的打包任务

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

我的问题是我在下面指示 ERROR 的行给出了错误,因为 std::thread 构造函数计算出要调用的函数并要求 函数签名所需的参数。

有什么办法可以解决吗?如果我尝试从 packaged_task 解码函数名称和参数列表,那么我不能在打包任务上使用 get_future 函数,需要添加我自己的 promise/future 代码来处理这个。

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

}

这应该有效。

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

您正在从空函数构造二进制 packaged_task (std::packaged_task<int(int,int)>),这是您绑定 (std::function<int()>) 的结果。

你要么不使用 bind,要么让 asyncExecutor 接受一个 nullary packaged_task (std::packaged_task<T()>)

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

template<typename T, typename ... ARGS> T asyncExecutor(std::packaged_task<T(ARGS...)>&& package, ARGS ... args)
{
    std::future<T> 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()> task(std::bind(sampleAddFunction, 10, 20));
    int testResult = asyncExecutor(std::move(task));
    cout << "testResult = " << testResult << endl;
    std::packaged_task<int(int,int)> task2(sampleAddFunction);
    int testResult2 = asyncExecutor(std::move(task2), 15, 27);
    cout << "testResult2 = " << testResult2 << endl;
}