1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #include <sys/time.h>
void timeConsumingSleep(int msTime)
{
timeval *startTime = (timeval *)malloc(sizeof(struct timeval));
timeval *curTime = (timeval *)malloc(sizeof (struct timeval));
long long startTimeAsLong = 0L;
long long curTimeAsLong = 0L;
long long endTimeAsLong = 0L;
if (startTime == NULL || curTime == NULL)
{
cerr << "Error creating time structure" << endl;
exit(EXIT_FAILURE);
}
gettimeofday(startTime, NULL);
startTimeAsLong = 1000L * startTime->tv_sec + startTime->tv_usec / 1000L;
endTimeAsLong = startTimeAsLong + (long)msTime;
curTimeAsLong = startTimeAsLong;
while(curTimeAsLong < endTimeAsLong)
{
gettimeofday(curTime, NULL);
curTimeAsLong = 1000L * curTime->tv_sec + curTime->tv_usec / 1000L;
}
free(startTime);
free(curTime);
startTime = NULL;
curTime = NULL;
} |
Partager