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

别来无恙 提交于 2021-01-03 06:25:24

问题


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"

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


回答1:


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

On a side note, implicitly casting uint64_t to size_t is not good. The latter could be of smaller size.



来源:https://stackoverflow.com/questions/21967574/stdasync-and-lambda-function-in-c-gives-no-associated-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!