简体   繁体   English

std::promise::set_value 崩溃

[英]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错误:在抛出 'std::future_error' what() 的实例后调用终止:std::future_error:没有关联的 state

g++ --version 7.5.0 however, the same lines of code runs fine on QNX with same g++ version. g++ --version 7.5.0 但是,相同的代码行在具有相同 g++ 版本的 QNX 上运行良好。

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. 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.我的确切问题是,如果这是预期的行为,有没有办法转移 promise object 的所有权,以便其他线程可以在稍后执行期间使用它。 Other than spawning a new thread, but to be able to access promise from an already existing thread.除了产生一个新线程,而是能够从一个已经存在的线程访问 promise。

#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.当您移动 promise 时,它会将共享的 state 移动到 promise 持有的移动到 ZA8CFDE6331BD59EB62AC96F89111 That leaves the moved from object with no shared state.这使得从 object 移出没有共享的 state。 If we check out the reference page of set_value() we see it states如果我们查看set_value()的参考页面,我们会看到它声明

Exceptions例外
std::future_error on the following conditions: std::future_error在以下情况下:

  • *this has no shared state. *this没有共享的 state。 The error category is set to no_state .错误类别设置为no_state

If your QNX implementation is not throwing an exception, then it is non-conforming.如果您的 QNX 实现没有抛出异常,那么它就是不合格的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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