Timer operated instructions?

Hey all,

What's a good, clean way to start a timer and then get the program to do something after x amount of time has elapsed?

I guess you could do it with <time.h> and use difftime, but that's only precise to a second, which isn't exactly ideal for when I need to do things every half or quarter of a second.

Thanks!
Use the clock() function to get the number of milliseconds since the program started.
You have command Sleep in windows.h parameter of function is an integer value represent miliseconds.
So if I write:

Sleep (1000) somewhere in code means that program will stay for 1 sec.
I'd rather not stop the whole program though (which I think will happen with sleep). Ideally it just checks if a second or whatever has passed and then does other stuff. I think with clock() I can do that.

Is there anyway to reset the clock though?

**Nvm with mod I can get something similar
Last edited on
You can store the value of clock() and then check the difference later.
If you can link with glibc, then you can use 'gettimeofday', which stores time with very fine resolution.

closed account (3hM2Nwbp)
You might want to check out Boost's deadline_timer implementation.

http://www.boost.org/doc/libs/1_46_0/doc/html/boost_asio/reference/basic_deadline_timer/

* Especially if you're looking for an asynchronous wait.
Last edited on
Topic archived. No new replies allowed.