Reputation: 492
I am trying to do a very simply thing (yet spent hours without a result): Print one line, sleep a little, print a second line. I expect the below code to do that, however, it first sleeps then prints both lines simultaneously! Can anyone see what I'm missing here?
This is the entire code:
#include<thread>
#include<chrono>
int main() {
printf("%s","Wait.\n");
std::this_thread::sleep_for(std::chrono::seconds(3));
printf("%s","Thank you for waiting.");
}
Computer info: Mac 10.14.16, Editor: CLion
Upvotes: 1
Views: 247
Reputation: 135
printf doesnt necessarily flush the buffer, which is why you dont see it until the thread resumes from sleep (it does this because it notices the thread is inactive). To force a flush add fflush(stdout);
before the call to thread sleep.
Upvotes: 3