问题描述
每当方便时,我都在尝试通过使用异步来在程序中获得更好的性能.我的程序可以编译,但是每次使用包含异步调用的函数时,都会出现以下错误:
I'm trying to obtain a better performance in my program by using async whenever this is convenient. My program compiles, but I get the following error every time I use a function containing async calls:
C++ exception with description "No associated state"
我尝试使用lambda调用异步的方式例如如下:
The way I am trying to call async with a lambda is e.g. as follows:
auto f = [this](const Cursor& c){ return this->getAbsIndex(c); };
auto nodeAbsIndex = std::async(f,node); // node is const Cursor&
auto otherAbsIndex = std::async(f,other); // other too
size_t from = std::min(nodeAbsIndex.get(), otherAbsIndex.get());
size_t to = std::max(nodeAbsIndex.get(), otherAbsIndex.get());
要调用的函数的签名如下:
Signature of the function to call is as follows:
uint64_t getAbsIndex(const Cursor& c) const
我在这里做错了什么?感谢您的提示!迭戈
What am I doing wrong here? Thanks for any hints!Diego
推荐答案
您不能在同一将来两次致电get()
.仔细阅读文档(与valid()
有关的部分): http://en.cppreference .com/w/cpp/thread/future/get
You can't call get()
twice on the same future. Read documentation carefully (the part regarding valid()
): http://en.cppreference.com/w/cpp/thread/future/get
另一方面,将uint64_t
隐式转换为size_t
是不好的.后者的尺寸可能较小.
On a side note, implicitly casting uint64_t
to size_t
is not good. The latter could be of smaller size.
这篇关于C ++中的std :: async和lambda函数未提供任何关联状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!