2012-10-18 15 views
11

मैं बस जीसीसी 4.7.2 के साथ नई सी ++ 11 सुविधाओं में से कुछ कोशिश कर रहा हूं, हालांकि जब मैं एक सीजी गलती चलाने के लिए जाता हूं।सी ++ 11 Async seg गलती

$ ./a.out 
Message from main. 
terminate called after throwing an instance of 'std::system_error' 
    what(): Unknown error -1 
Aborted (core dumped) 

मैं ग के संबंध में जीसीसी के 'बीटा' सुविधाओं के साथ संकलित, ++ 0x के साथ:

g++ -std=c++11 c11.cpp 

कोड:

#include <future> 
#include <iostream> 

void called_from_async() { 
    std::cout << "Async call" << std::endl; 
} 

int main() { 
    //called_from_async launched in a separate thread if possible 
    std::future<void> result(std::async(called_from_async)); 

    std::cout << "Message from main." << std::endl; 

    //ensure that called_from_async is launched synchronously 
    //if it wasn't already launched 
    result.get(); 

    return 0; 
} 

उत्तर

22

मेरा मानना ​​है कि इस वजह से आप होता है POSIX धागे पुस्तकालय के साथ लिंक करने के लिए भूल गए हैं। बस -pthread या -lpthreadg++ झंडे जोड़ें और समस्या दूर होनी चाहिए।

यदि आप विवरण में रुचि रखते हैं, तो ऐसा इसलिए होता है क्योंकि C++ 11 रनटाइम केवल pthread से रन-टाइम में प्रतीकों को हल कर रहा है यदि आप उन सुविधाओं का उपयोग करते हैं। इसलिए यदि आप लिंक करना भूल गए हैं, तो रनटाइम उन प्रतीकों को हल करने में सक्षम नहीं होगा, अपने पर्यावरण का इलाज करें जैसे कि यह धागे का समर्थन नहीं करता है, और अपवाद फेंक देता है (जिसे आप नहीं पकड़ते हैं और यह आपके आवेदन को रोकता है)।

+0

+1। आपको बहुत - बहुत धन्यवाद! – TheBlueCat

+2

एफडब्ल्यूआईडब्ल्यू, जोड़ना-अकेलापन अकेले काम नहीं करता था, लेकिन -छाई ने किया। –