Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fixed-time Pausing in C

Status
Not open for further replies.

Radeon

Programmer
Jul 12, 2000
7
US
Is there a command to pause for a specified interval in C?
 
Use the clock function defined in time.h<br><br>void Sleep( clock_t wait )<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;clock_t goal;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;goal = wait + clock();<br>&nbsp;&nbsp;&nbsp;&nbsp;while( goal &gt; clock() )<br> ;<br>}<br><br> <p> <br><a href=mailto:Kim_Christensen@telus.net>Kim_Christensen@telus.net</a><br><a href= Page</a><br>
 
Not sure if the above can handle anything smaller than seconds, but if you need to wait for a granularity of time less than one second you can use select():<br><br><FONT FACE=monospace>include &lt;sys/time.h&gt;<br><br>void main<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;struct timeval timeout;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;/* set timeout to 0 seconds and 200 milliseconds */<br>&nbsp;&nbsp;&nbsp;&nbsp;timeout.tv_sec = 0;<br>&nbsp;&nbsp;&nbsp;&nbsp;timeout.tv_usec = 200;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;/* waits for 200 milliseconds */<br>&nbsp;&nbsp;&nbsp;&nbsp;select(NULL, NULL, NULL, &timeout);<br>}</font><br><br>Hope that helps!<br>Loon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top