【发布时间】:2020-02-24 17:45:13
【问题描述】:
请注意,这个问题在here 和here 有一个“答案”,但我的问题不是关于如何摆脱错误,而是为什么会出现这个错误。
考虑以下代码:
include <cstdio>
#include <future>
int main() {
std::promise<int> promise;
auto future = promise.get_future();
promise.set_value(42);
auto result = future.get();
printf("%d\n", result);
}
这段代码抛出异常:
$ g++ -g -std=c++1z main.cpp
$ ./a.out
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
Aborted (core dumped)
解决方法是在命令行传递-lpthread:
$ g++ -g -std=c++1z -lpthread main.cpp
$ ./a.out
42
现在,当我不链接所需的库时,我习惯于收到链接错误。这是我第一次遇到运行时错误。
当您在 gdb 下运行版本(不带 lpthread)时,这是您将获得的堆栈跟踪(已编辑部分内容):
#5 0x00007ffff7aa6ef3 in __cxxabiv1::__cxa_throw (obj=obj@entry=0x61ad00,
tinfo=tinfo@entry=0x7ffff7dce6b0 <typeinfo for std::system_error>,
dest=dest@entry=0x7ffff7ad02b0 <std::system_error::~system_error()>)
at /tmp/tmp.kmkSDUDFn8/build/../gcc-9.1.0/libstdc++-v3/libsupc++/eh_throw.cc:95
#6 0x00007ffff7a9d0ec in std::__throw_system_error (__i=-1)
at /tmp/tmp.kmkSDUDFn8/build/x86_64-pc-linux-gnu/libstdc++-v3/include/ext/new_allocator.h:89
#7 0x000000000040240f 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=
@0x7fffffffdd80: (void (std::__future_base::_State_baseV2::*)(class std::__future_base::_State_baseV2 * const, class std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter>()> *, bool *)) 0x40200c <std::__future_base::_State_baseV2::_M_do_set(std::function<std::unique_ptr<std::__future_base::_Result_base, std::__future_base::_Result_base::_Deleter> ()>*, bool*)>, __args#0=@0x7fffffffdd78: 0x61ac30, __args#1=@0x7fffffffdd70: 0x7fffffffddf0,
__args#2=@0x7fffffffdd68: 0x7fffffffdd67) at /.../include/c++/9.1.0/mutex:697
#8 0x0000000000401e5d 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 /.../include/c++/9.1.0/future:401
所以它与call_once 有关。很好奇为什么这表现为运行时错误而不是链接时间。
【问题讨论】:
-
你应该编译和链接
-pthread,而不是-lpthread(stackoverflow.com/questions/2127797/…) -
@Mat
-pthread也可以。但问题是专门询问为什么我们在运行时而不是链接时收到此错误。