在std :: promise :: set_value崩溃中崩溃 [英] Crash in std::promise::set_value crash

查看:30
本文介绍了在std :: promise :: set_value崩溃中崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误:抛出'std :: future_error'实例后终止调用what():std :: future_error:没有关联状态

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

g ++-版本7.5.0但是,相同的代码行可以在具有相同g ++版本的QNX上正常运行.

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

此外,如果我们产生一个新线程并将诺言作为参数移至该线程函数,则在诺言对象上调用set_value()时不会出现异常.

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.

我的确切问题是,如果这是预期的行为,有没有一种方法可以转移promise对象的所有权,以便其他线程可以在执行过程中的稍后时间使用它.除了产生新线程外,还可以从已经存在的线程中访问promise.

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

推荐答案

在这里获取异常是标准行为.移动承诺时,它会将承诺保留的共享状态移动到已移动到的对象.从对象移出的对象没有共享状态.如果我们查看 set_value()参考页我们看到了它的状态

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

例外
std :: future_error 在以下情况下:

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

如果您的QNX实现未引发异常,则说明它不符合要求.

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

这篇关于在std :: promise :: set_value崩溃中崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆