void wait ( int seconds )
  clock_t endwait;
  endwait = clock () + seconds * CLOCKS_PER_SEC ;
  while (clock() < endwait) {}
9 Years
Discussion Span
Last Post by McQueen

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!


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.