2017-05-12 8 views
5

से अज्ञात अपवाद निम्नलिखित कोड में क्या गलत है? भागा कार्यक्रम एक अज्ञात अपवाद के साथ रोकता जबstd :: वचन

#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. 

एक ही कोड पर एक मैक बस ठीक काम करता है देता है


नोट कोड की लाइन अपवाद निकलती है वह यह है कि से promise.set_value(1)

+0

के लिए @ vu1p3n0x एक खाली साझा राज्य का संकेत नहीं दे कि अगले get_future फेंक देते हैं। इसके अलावा set_value जो किसी कारण से यहां फेंकता है .. – Curious

+0

चीज़ पर एक डीबगर डालें और उस अपवाद को फेंकने के बारे में सटीक जानकारी दें। कोड के साथ कुछ भी गलत नहीं है, और मैं वंडबॉक्स के जीसीसी 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 

डंप से , हम इसे म्यूट का उपयोग कर देख सकते हैं एक्स, आदि तब मुझे एहसास हुआ कि std::future सामान थ्रेड पर निर्भर करता है, इसलिए इसे pthread के विरुद्ध लिंक करने की आवश्यकता है, अन्यथा हम यह अपवाद देखते हैं।

ही std::thread

+0

वाह, मुझे याद रखना चाहिए था कि आपको कई प्रणालियों पर '-प्थ्रेड' ध्वज की आवश्यकता है – Curious

संबंधित मुद्दे