gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 05:29:08 25 4
gpt4 key购买 nike

如何让 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 指令和 using 行中切换注释,并使用

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

我得到了想要的输出:

in f 3
f 4

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

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

最佳答案

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

关于c++ - 如何将函数参数传递给 boost::async(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36529379/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com