【问题标题】:ability to run a packaged task with std::bind function parameters in a seperate thread via template function能够通过模板函数在单独的线程中运行带有 std::bind 函数参数的打包任务
【发布时间】:2017-06-21 05:08:11
【问题描述】:

我的问题是我在下面指示 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;

}

【问题讨论】:

    标签: multithreading c++11 templates stdbind


    【解决方案1】:

    这应该可行。

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

    【讨论】:

      【解决方案2】:

      您正在从一个空函数构造一个二进制 packaged_task (std::packaged_task&lt;int(int,int)&gt;),这是您绑定 (std::function&lt;int()&gt;) 的结果。

      您不应该使用 bind,或者让 asyncExecutor 接受一个空的 packaged_task (std::packaged_task&lt;T()&gt;)

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-18
        • 2019-04-30
        • 1970-01-01
        • 2016-04-26
        • 1970-01-01
        • 2014-01-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多