Thursday, July 9, 2009

How can i use clocks or timing in C programming ?

listen for instance i want to write a program witch scans a charachter every 5 seconds and if you dont enter any thing for say 2 seconds itll continue doing another job then asks again after 5 seconds and so on

How can i use clocks or timing in C programming ?
unsigned sleep(unsigned seconds);





The sleep() function shall cause the calling thread to be suspended from execution until either the number of realtime seconds specified by the argument seconds has elapsed or a signal is delivered to the calling thread and its action is to invoke a signal-catching function or to terminate the process.
Reply:You could use a global boolean variable that sets whether to run the other job or not.





The variable could be turned to false by the other job after 5 seconds, for example.





To check when to turn the variable on/off you can use the ctime library and set a variable to the current clock when you start the job and compare it to current clock time periodically within the job (which also checks periodically whether the boolean value is set to false). Whenever the difference is 5 seconds, set the boolean value to false.


No comments:

Post a Comment