Timer operated instructions?

Jun 28, 2011 at 9:15pm
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!
Jun 28, 2011 at 9:17pm
Use the clock() function to get the number of milliseconds since the program started.
Jun 28, 2011 at 9:24pm
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.
Jun 28, 2011 at 9:28pm
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 Jun 28, 2011 at 9:31pm
Jun 28, 2011 at 9:33pm
You can store the value of clock() and then check the difference later.
Jun 28, 2011 at 10:49pm
If you can link with glibc, then you can use 'gettimeofday', which stores time with very fine resolution.

Jun 28, 2011 at 11:10pm
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 Jun 28, 2011 at 11:10pm
Topic archived. No new replies allowed.