Tóm lại, thêm -pthread
giải quyết sự cố của bạn.
$ g++ -std=c++14 -g -pthread -o temp temp.cpp
$ ./temp
Chi tiết
tôi có thể mô phỏng hành vi trên Ubuntu 16.04 với bên dưới lệnh về soạn thảo:
$ 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 đổ chương trình:
(gdb) bt
#0 0x00007ffff74ab428 in __GI_raise ([email protected]=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
Từ bãi , chúng ta có thể thấy nó sử dụng câm x, vv Sau đó, tôi nhận ra rằng std::future
công cụ phụ thuộc vào chủ đề, vì vậy nó cần phải liên kết với pthread
, nếu không chúng tôi thấy ngoại lệ này.
Tương tự cho std::thread
@ vu1p3n0x trạng thái chia sẻ trống không được ngụ ý rằng get_future tiếp theo sẽ bị ném. Cũng là set_value của nó mà ném ở đây vì một lý do nào đó .. – Curious
Đặt một trình gỡ lỗi trên vật và tìm ra chính xác những gì đang ném ngoại lệ đó. Không có gì sai với mã, và tôi không thể sao chép ngoại lệ được báo cáo trên GCC 5.4 của Wandbox. –