I have recently started exploring C++11 threads. I am using eclipse for sample programs. Output in the Eclipse console is confusing me.
When I make the detached thread wait for a second, I don't see the output from detached thread in the console.
If I don't make detached thread wait, console output seems proper.
#include<iostream>
#include<thread>
#include<chrono>
using std::cout;
using std::endl;
using std::thread;
void hello(){
std::this_thread::sleep_for(std::chrono::seconds(5));
cout << "hello from hello!" << endl;
}
int main(){
thread(hello).detach();
cout << "hello from main" << endl;
return 0;
}
Console output: hello from main
If hello method is as followed, output seems okay.
void hello(){
//std::this_thread::sleep_for(std::chrono::seconds(1));
cout << "\nhello from method!" << endl;
}
Console output: hello from main hello from method!
Could someone please help me understand what is happening here?