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

asynchronous, c++, c++11, exception, lambda

Solution

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.

Problem

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

Original source