#include <cstdlib>
#include <thread>
#include <chrono>
#include <iostream>
using namespace std;
using namespace std::literals;
struct A
{
int n_ = 0;
A(int n) : n_(n) { cout << "A:" << n_ << endl; }
~A() { cout << "~A:" << n_ << endl; }
};
A a1(1);
int main()
{
std::thread([]()
{
static A a2(2);
thread_local A a3(3);
std::this_thread::sleep_for(24h);
}).detach();
static A a4(4);
thread_local A a5(5);
std::this_thread::sleep_for(1s);
std::exit(0);
}
मेरा कंपाइलर clang 5.0
-std=c++1z
के साथ है।`std :: exit` क्यों उम्मीदवारों को अपेक्षित रूप से ट्रिगर नहीं करता है?
उत्पादन इस प्रकार है: है
A:1 A:2 A:4 A:5 A:3 ~A:5 ~A:2 ~A:4 ~A:1
ध्यान दें कि वहाँ कोई ~A:3
है, जो वस्तु A a3
विलुप्त नहीं किया गया है।
हालांकि, cppref के अनुसार:
std::exit
सामान्य कार्यक्रम समाप्ति होने के लिए कारण बनता है। कई सफाई कदम प्रदर्शन किए गए:थ्रेड स्थानीय भंडारण अवधि के साथ वस्तुओं के विनाशक ... कहने की गारंटी है।
क्योंकि आप इसे अलग करते हैं, मुझे लगता है। – SingerOfTheFall
क्या यह http://stackoverflow.com/questions/19744250/what-happens-to-a-detached-thread-when-main-exits का डुप्लिकेट है? –
नहीं। यह वही है भले ही थ्रेड अलग नहीं हो। – xmllmx