gpt4 book ai didi

c++ - C++ 中的 std::async 和 lambda 函数不提供关联状态

转载 作者:太空狗 更新时间:2023-10-29 19:58:24 25 4
gpt4 key购买 nike

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

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

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

最佳答案

您不能在同一个 future 调用 get() 两次。仔细阅读文档(关于 valid() 的部分):http://en.cppreference.com/w/cpp/thread/future/get

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

关于c++ - C++ 中的 std::async 和 lambda 函数不提供关联状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21967574/

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