简体   繁体   中英

Crash in std::promise::set_value crash

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!
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM