std::async 和 lambda 函数在C++中没有给出关联的状态

std::async and lambda function in C++ gives no associated state

本文关键字:出关 状态 关联 C++ async lambda 函数 std      更新时间:2023-10-16

我试图通过在方便时使用异步在我的程序中获得更好的性能。我的程序可以编译,但每次使用包含异步调用的函数时都会收到以下错误:

C++ exception with description "No associated state"

我尝试用lambda调用异步的方式如下:

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());

要调用的函数的签名如下:

uint64_t getAbsIndex(const Cursor& c) const

我在这里做错了什么?感谢您的任何提示!迭 戈

你不能

在同一个未来给get()打电话两次。仔细阅读文档(有关valid()的部分):http://en.cppreference.com/w/cpp/thread/future/get

附带说明一下,隐式地将uint64_t转换为size_t是不好的。后者的尺寸可能较小。