Time Delay function
Posted: Wed Dec 25, 2013 2:57 am
Code: Select all
//=====================================================================//
/*
* delay:
* Wait for some number of milliseconds
*********************************************************************************
*/
void delay (unsigned int howLong)
{
struct timespec sleeper, dummy ;
sleeper.tv_sec = (time_t)(howLong / 1000) ;
sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
nanosleep (&sleeper, &dummy) ;
}sleeper.tv_sec=(time_t)(50/1000); that's equal to 0.05 seconds
sleeper.tv_nsec=(long)(50%1000)*1000000 that's equal to 50*1000000= 500,000,000
or 5 nanoseconds.
questions, why does sleeper needs both second and nanosecond fields? Seem like they are holding to same values.
second question why does tv_sec uses a divide function / and tv_nsec uses a modulus ,% function?