使用用户定义的参数调用future/async并调用类方法
Calling future / async with user defined parameter and invoking a class method
是否可以调用async函数并使用类方法返回值:
void A::GetReply()
{
auto fn = std::async([this](const struct mydata& msg)
{
oncall(msg);
});
}
int A::onReply(const struct mydata& msg)
{
return msg.value;
}
我得到编译错误:
6>: error C2672: 'std::async': no matching overloaded function found
6>: error C2893: Failed to specialize function template 'std::future<_Invoke_traits<void,_Callable,decay<_ArgTypes>::type...>::type> std::async(_Fty &&,_ArgTypes &&...)'
6> with
6> [
6> _Callable=decay<_Ty>::type
6> ]
6>: note: With the following template arguments:
6>: note: '_Fty=A::{ctor}::<lambda_75cbb6e549dc12613fd9546c1d31aa61>'
6>: note: '_ArgTypes={}'
6>: error C2780: 'std::future<_Invoke_traits<void,_Callable,decay<_ArgTypes>::type...>::type> std::async(std::launch,_Fty &&,_ArgTypes &&...)': expects 3 arguments - 1 provided
6> with
6> [
6> _Callable=decay<_Ty>::type
6> ]
6>c:program files (x86)microsoft visual studio2017professionalvctoolsmsvc14.15.26726includefuture(1821): note: see declaration of 'std::async'
实现"future"的函数调用(以异步方式启动(并获取异步函数调用的返回值的正确方法是什么?
的问题
auto fn = std::async([this](const struct mydata& msg)
{
oncall(msg);
});
是lambda需要一个参数,但没有将其传递给async
。您必须向async
传递要异步运行的函数及其所有参数。如果msg
是该类的成员,则可以将签名更改为
auto fn = std::async([this]()
{
oncall(msg);
});
则CCD_ 4为CCD_。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 使用用户定义的参数调用future/async并调用类方法
- C++ std::future没有被调用
- 如何在 std::future 的向量中调用异步函数
- 线程是否真的在调用 std::future::get() 后启动
- 是否可以在调用 future.get() 之前销毁 std::p romise ?
- 程序在使用异步的 Boost.Process 调用 std::future.get() 后挂起
- 在 std::future 上多次从多个线程调用 wait() 是否安全
- std::future.get() 多个调用(来自不同的线程)
- std::future::wait 应该使用这么多 CPU 吗?是否有性能更高的调用
- boost::future-是否保证wait_callback只被调用一次
- 为什么我的带有 boost::future 的 .then 的链式方法没有被调用?
- 重复调用std::future::get
- 为什么我可以多次调用boost::unique_future::get,而不像std::future