2017-05-12 1 views
5

다음 코드에 어떤 문제가 있습니까? 프로그램이 알 수없는 예외 중단 실행할 때 나를std :: promise에서 알 수없는 예외가 발생했습니다.

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에서 잘 작동 제공을 위해

#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입니다


예외가 짧은에서 promise.set_value(1)

+0

에 대한

동일 vu1p3n0x @ 빈 공유 상태는 다음 get_future가 발생합니다 것을 의미하지는한다. 또한 어떤 이유로 여기에 던져지는 set_value .. – Curious

+0

일에 디버거를 넣고 그 예외를 던지고있는 것이 무엇인지 정확하게 파악하십시오. 코드에 아무 문제가 없으며 Wandbox의 GCC 5.4에서보고 된 예외를 재현 할 수 없습니다. –

답변

6

이다에서 유래 코드의 라인, -pthread을 추가하여 문제를 해결합니다.

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

세부

내가 컴파일에 명령 아래로 우분투 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 ([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 

를 덤프에서 , 음소거를 사용하고있는 것을 볼 수 있습니다. x 등등. 그럼 내가 std::future 물건은 스레드에 따라 다르므로 pthread에 링크해야합니다. 그렇지 않으면이 예외가 표시됩니다. std::thread

+0

와우, 나는 많은 시스템에서'-pthread' 플래그가 필요하다는 것을 기억해야했다. – Curious

관련 문제