This setup
void run()
{
while (true)
{
std::cout << "Hello, Thread!\n";
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
void foo()
{
std::thread t(run);
t.detach();
//std::this_thread::sleep_for(std::chrono::seconds(3));
}
int main()
{
foo();
getchar();
}
Gives me no output until I press enter (getchar returns and program ends, but I can see the output for a short while). However, when use the out commented line from foo, the output is shown directly. (Even after foo returns.) I'm using the VS11 beta version. Which behavior is required here according to the standard?
"\n" doesn't flush the output buffer. You need to use std::endl instead which flushes the buffer.
void run()
{
while (true)
{
std::cout << "Hello, Thread!"<<std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
You could try using std::flush to manually flush the cout buffer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With