You are not logged in.
Hi there, im observing issue with usleep. For example in this code:
#include <stdio.h>
#include <iostream>
#include <unistd.h>
using namespace std;
int main() {
for(int i=0;i<100;i++) {
cout<<i+1;
unsigned int sleep_time = 100000;
usleep(sleep_time);
}
return 0;
}
The problem:
program wont write output then sleep then next output, instead it waits a long time then shows all outputs in once.
What should i do?
Last edited by mazhar (2016-08-01 16:47:30)
We are archers, we live in dark to serve the light.
Offline
no, you're observing buffered output
Offline
Just add << std::flush to cout line.
Offline
Just add << std::flush to cout line.
Thanks, it totally works. is this an obvious concept? what should i read about it?
We are archers, we live in dark to serve the light.
Offline
you're observing buffered output
It would also "work" if you added a newline after each number.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline