In the below C++ program, I am using the function usleep()
to sleep for a 1.5 seconds. I implemented that in 2 equivalent methods as illustrated below:
#include <iostream>
#include <unistd.h>
using namespace std;
int main() {
//METHOD #1
cout<<"sleep"<<endl;
usleep(1500000);
cout<<"wake up"<<endl;
//METHOD #2
cout<<"sleep"<<endl;
for(int i=0; i<1500000; i++)
usleep(1);
cout<<"wake up"<<endl;
return 0;
}
however the results came as follows:
- First method: takes exactly 1.5 seconds
- Second method: takes around 1.5 minutes !
Actually, I will need the second method. According to this Answer, I think I need a more accurate function that usleep()
. Could any one help ?