-1

error: terminate called after throwing an instance of 'std::future_error' what(): std::future_error: No associated state

g++ --version 7.5.0 however, the same lines of code runs fine on QNX with same g++ version.

Also, if we spawn a new thread and move promise to that thread function as a parameter, then exception is not theown when set_value() is invoked on promise object.

My exact question being, if this is an expected behavior, Is there a way to transfer ownership of promise object so that some other thread can use it at a later point of time during execution. Other than spawning a new thread, but to be able to access promise from an already existing thread.

#include <future>

using namespace std;

int main()
{
    promise<int> p1;
    promise<int> p2(move(p1));
    p1.set_value(99); // SIGSEGV!
}
4
  • 2
    Wait a second... you've moved p1 to p2 and then set the state on p1? What is that supposed to do?
    – Ted Lyngmo
    Feb 10, 2021 at 16:51
  • 1
    There's no crash here. An exception was thrown. std::terminate was called. A message describing the problem was displayed. What exactly is your question? Are you asking why it throws? Why terminate is called? How to achieve your intended task without throwing? Feb 10, 2021 at 16:52
  • I believe throw/terminate is the correct behaviour. You never created a future for the promise, so what exactly set_value is supposed to do?
    – ALX23z
    Feb 10, 2021 at 16:54
  • @Guruprasad Doesn't the answer you've gotten answer your question?
    – Ted Lyngmo
    Feb 11, 2021 at 16:32

1 Answer 1

1

Getting an exception is the standard behavior here. When you move a promise, it moves the shared state the the promise holds to the moved to object. That leaves the moved from object with no shared state. If we check out the reference page of set_value() we see it states

Exceptions
std::future_error on the following conditions:

  • *this has no shared state. The error category is set to no_state.

If your QNX implementation is not throwing an exception, then it is non-conforming.

4
  • I am not entirely sure if it is non-conforming as a moved-from state can in general be an invalid state with normal member functions behaving inappropriately (aside from destruction and copy/move assignment). So technically this is just a UB. Then again, leaving an object in an invalid state is also a bad practice and poor STL implementation.
    – ALX23z
    Feb 10, 2021 at 17:00
  • 1
    @ALX23z The standard requires moved from object to be in an unspecified but valid state. If that's not happening, then it is one more bit of broken implementation that needs to be fixed. Feb 10, 2021 at 17:02
  • I remember a certain ambiguity about various meanings of valid/invalid/unspecified state. Where in every place it means something else. IIRC only destructor and copy/move assignment are strictly necessary while other operations might cause issues in moved-from state.
    – ALX23z
    Feb 10, 2021 at 17:10
  • 4
    @ALX23z Unless otherwise specified. The standard specifies that after being moved from, the promise will have no shared state. The standard also specifies that if the promise doesn't have any shared state when calling set_value, then an exception will be thrown. So, this code must throw an exception. To not do so is non-conforming. Feb 10, 2021 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.