【问题标题】:How to pass function argument to boost::async()如何将函数参数传递给 boost::async()
【发布时间】:2016-04-10 11:55:37
【问题描述】:

如何让boost::async 将函数参数传递给它执行的函数。我的代码是

#include <iostream>

// This is for the boos version
#include <boost/thread/future.hpp>
using boost::async;

// This is for the standard library version
//~ #include <future>
//~ using std::async;

int f(int x) {
   std::cout << " in f " << x << std::endl;
   return x+1;
}

int main() {
    auto f_res = async(f,3);
    std::cout << "f " << f_res.get() << std::endl;
    return 0;
}

我用

编译
g++ -std=c++14 -lboost_thread -lboost_system -lpthread test_async_boost.cc

,g++ version 5.3.0 出现很多错误,抱怨async愿意接受的参数数量:

test_async_boost_2.cc: In function 'int main()':
test_async_boost_2.cc:16:26: error: no matching function for call to 'async(int (&)(int), int)'
    auto f_res = async(f,3);
                          ^
In file included from test_async_boost_2.cc:3:0:
/usr/include/boost/thread/future.hpp:4035:3: note: candidate: template<class F> boost::unique_future<typename boost::result_of<F()>::type> boost::async(F&&)
   async(BOOST_THREAD_FWD_REF(F) f) {
   ^
/usr/include/boost/thread/future.hpp:4035:3: note:   template argument deduction/substitution failed:
test_async_boost_2.cc:16:26: note:   candidate expects 1 argument, 2 provided
    auto f_res = async(f,3);
                          ^
In file included from test_async_boost_2.cc:3:0:
/usr/include/boost/thread/future.hpp:4018:3: note: candidate: template<class R> boost::unique_future<T> boost::async(R (*)())
   async(R(*f)()) {
   ^
/usr/include/boost/thread/future.hpp:4018:3: note:   template argument deduction/substitution failed:
test_async_boost_2.cc:16:26: note:   candidate expects 0 arguments, 1 provided
    auto f_res = async(f,3);
                          ^
In file included from test_async_boost_2.cc:3:0:
/usr/include/boost/thread/future.hpp:3695:3: note: candidate: template<class F> boost::unique_future<typename boost::result_of<typename boost::decay<T>::type()>::type> boost::async(boost::launch, F&&)
   async(launch policy, BOOST_THREAD_FWD_REF(F) f) {
   ^
/usr/include/boost/thread/future.hpp:3695:3: note:   template argument deduction/substitution failed:
test_async_boost_2.cc:16:26: note:   cannot convert 'f' (type 'int(int)') to type 'boost::launch'
    auto f_res = async(f,3);
                          ^
In file included from test_async_boost_2.cc:3:0:
/usr/include/boost/thread/future.hpp:3634:3: note: candidate: template<class R> boost::unique_future<T> boost::async(boost::launch, R (*)())
   async(launch policy, R(*f)()) {
   ^
/usr/include/boost/thread/future.hpp:3634:3: note:   template argument deduction/substitution failed:
test_async_boost_2.cc:16:26: note:   cannot convert 'f' (type 'int(int)') to type 'boost::launch'
    auto f_res = async(f,3);
                          ^

如果我在#includedirectuves 和using 行中切换注释,并使用

进行编译
g++ -std=c++14 test_async_boost_2.cc -lpthread

我得到了想要的输出:

in f 3
f 4

如何让boost::async 使用函数参数?

还有:我在哪里可以找到boos::async 的参考文档?

【问题讨论】:

  • @Zereges:这不是说参数必须在编译时知道吗?
  • @Zereges:刚刚试了一下,效果很好。请给它一个答案;)

标签: c++ asynchronous boost c++14


【解决方案1】:

为了将函数及其参数传递给接受函数的方法,您必须使用std::bindboost::bind

【讨论】:

    猜你喜欢
    • 2013-04-25
    • 2011-08-09
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多