如何将函数参数传递给boost :: async()

时间:2016-04-10 11:55:38

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

如何让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 ++版本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);
                          ^

如果我在#include directuves和using行中切换评论,并使用

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

我得到了所需的输出:

in f 3
f 4

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

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

1 个答案:

答案 0 :(得分:1)

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