C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 来自std :: promise的未知异常大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
以下代码有什么问题?运行时,程序以未知异常中止
#include <iostream>
#include <future>

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

    return 0;
}
@H_301_4@错误输出

terminate called after throwing an instance of 'std::system_error'
  what():  UnkNown error -1
Aborted (core dumped)
@H_301_4@g – 版本给我

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.
@H_301_4@相同的代码在mac上运行得很好

@H_301_4@注意异常源自的代码行是promise.set_value(1)

解决方法

简而言之,添加-pthread可以解决您的问题.
$g++ -std=c++14 -g -pthread -o temp temp.cpp
$./temp
@H_301_4@细节

@H_301_4@我可以在编译时使用以下命令重现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)
@H_301_4@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,bool*>(std::once_flag&,void (std::__future_base::_State_baseV2::*&&)(std::function<std::unique_ptr<std::__future_base::_ResulT_Base,std::__future_base::_State_baseV2*&&,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,DIE 0xeb8a>) at /usr/include/c++/5/future:1075
#10 0x0000000000401759 in main () at temp.cpp:7
@H_301_4@从转储中,我们可以看到它正在使用互斥锁等.
然后我意识到std :: future的东西依赖于线程,所以它需要链接到pthread,否则我们会看到这个异常.

@H_301_4@对于std :: thread也一样

大佬总结

以上是大佬教程为你收集整理的c – 来自std :: promise的未知异常全部内容,希望文章能够帮你解决c – 来自std :: promise的未知异常所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。