Diego H.
Diego H.

Reputation: 863

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

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

Upvotes: 3

Views: 6644

Answers (1)

mike.dld
mike.dld

Reputation: 3049

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.

Upvotes: 12

Related Questions