c++ - 来自 std::promise 的未知异常

标签 c++ c++11 promise c++14 future

下面的代码有什么问题?运行时程序因未知异常而中止

#include <iostream>
#include <future>

int main() {
    auto promise = std::promise<int>{};
    auto future_one = promise.get_future();
    promise.set_value(1);

    return 0;
}

错误输出为

terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1
Aborted (core dumped)

g++ --version 对我来说

g++ (Ubuntu 5.4.0-6ubuntu1~16.04.2) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

同样的代码在 mac 上工作得很好


注意异常产生的代码行是promise.set_value(1)

最佳答案

简而言之,添加 -pthread 可以解决您的问题。

$ g++ -std=c++14 -g -pthread -o temp temp.cpp
$ ./temp

详情

我可以在 Ubuntu 16.04 上使用以下编译命令重现该行为:

$ g++ -std=c++14 -g -o temp temp.cpp
$ ./temp
terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1
Aborted (core dumped)

GDB 转储显示:

(gdb) bt
#0  0x00007ffff74ab428 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:54
#1  0x00007ffff74ad02a in __GI_abort () at abort.c:89
#2  0x00007ffff7ae484d in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff7ae26b6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007ffff7ae2701 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007ffff7ae2919 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff7b0b7fe in std::__throw_system_error(int) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x000000000040259b in std::call_once<void (std::__future_base::_State_baseV2::*)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*>(std::once_flag&, void (std::__future_base::_State_baseV2::*&&)(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*), std::__future_base::_State_baseV2*&&, std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*&&, bool*&&) (__once=...,
    __f=<unknown type in /home/mine/tempdir/temp, CU 0x0, DIE 0xe578>) at /usr/include/c++/5/mutex:746
#8  0x0000000000401e06 in std::__future_base::_State_baseV2::_M_set_result(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>, bool) (this=0x61ac30, __res=..., __ignore_failure=false) at /usr/include/c++/5/future:387
#9  0x0000000000402aee in std::promise<int>::set_value(int&&) (this=0x7fffffffe1c0, __r=<unknown type in /home/mine/tempdir/temp, CU 0x0, DIE 0xeb8a>) at /usr/include/c++/5/future:1075
#10 0x0000000000401759 in main () at temp.cpp:7

从转储中,我们可以看到它正在使用互斥锁等。 然后我意识到 std::future 的东西依赖于线程,所以它需要链接到 pthread,否则我们会看到这个异常。

std::thread 相同

关于c++ - 来自 std::promise 的未知异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43928715/

相关文章:

python - python调用so文件时如何在extern C中使用shared_ptr?

c++ - 检查成员是否存在并调用它或什么也不做

javascript - $.当 promise 解决得太早时

angularjs - 在 Angular 中,promise 的 error 和 catch 函数在概念上的区别是什么?

c++ - 我正在尝试在我的 SFML Platformer 中实现双跳,但它没有按预期方式工作

c++ - 库 Poco : DSO missing from command line

c++ - 使用 atan2 将 - 1 到 1 的范围转换为度数

c++ - 在初始化指向同一数组的指针之前初始化数组

C++ 只读取文件中的随机行

javascript - 对 Promise 处理的插入/创建进行 Sequelize 外键约束