The sub-thread and main thread cleanup'clean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <iostream>
#include <thread>
#include <string>
struct A
{
std::string message;
public:
A(const std::string& m):message(m){}
~A(){std::cout<<message<<std::endl;}
};
A a("123456");
int main()
{
std::thread t([](){A a("6543321");while(true){}});
t.detach();
//std::cin.get();
return 0;
}
|
//
Why is there not "654321" output when the programmer is over?
//I use the command window, the result as the following:
C:\Users\wzh\Documents\Visual Studio 2012\Projects\ConsoleApplication17\Release>test.exe
123456
C:\Users\wzh\Documents\Visual Studio 2012\Projects\ConsoleApplication17\Release>
Because the thread does not end due to the while(true){}
. It will be aborted at the end of main.
Topic archived. No new replies allowed.