void wait ( int seconds )
{
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
}

Recommended Answers

All 3 Replies

it sets endwait to be the current clock() value (presumably a timestamp) + a time to wait. It then goes into an empty loop until the clock() value becomes equal to endwait, at which point it exits the loop and continues. This technique is also known as a spinlock (link goes into some detail, certainly not required reading :icon_wink:)

Hi McQueen, basically all this code does is "pause" the program for a given number of seconds...The clock_t type name is part of the ctime header and is used when returning clock() variables (clock() is a function of the ctime header file)...the clock() function returns the number of clock ticks elapsed since the program was launched. the CLOCKS_PER_SEC variable is a const that specifies clock ticks per second...thus, the endwait variable is set to the number of clock ticks since the program started + the number of clock ticks to wait (based on a given number of seconds)...the while() loop iterates (do nothing on each iteration) until the requisite number of seconds (clock ticks) have passed...After the requisite time, the while loop will end and the function will complete...
Hope this helps!

Cheers!
-Eric

Thanks a lot! ^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.