(06-17-2013, 06:54 AM)Psycho_Coder Wrote: I think its better to learn from documentation than books as books doesn't covers every detail.
Thank You
Sincerely
Psycho_Coder
Books written by 3rd party "people", can be good for conceptual stuff however, as documentation doesn't really provide this (usually). This is assuming that it's a reputable author though as there are some out there, as obviously I'm sure you know yourself, in which have been criticized for misleading or inaccurate information. When you find a good book though, it's always nice to see what's "going on".
(06-17-2013, 03:32 PM)RootTheSystem Wrote: What should I do then?
I don't know enough about C to confidentally answer this. I could probably provide a solution, but some C "buff" might come along and say I'm wrong because I'm not using standard C methods or libraries, or it's not portable, whatever...
You probably don't care about portability? This is Windows you want the timer for right? Do you want a threaded timer? A timer with callbacks to do other stuff? What are your requirements here exactly?
Because you can end up making some pretty advanced timers that run on threadpools. Or just a timer that runs on the thread it is called from and in which just invokes something each "tick".
Lots of people are recommending clock() though because it is ANSI standard (time.h). However, for concurrency, you can only run one timer at a time unless you create some kind of interrupt strategy, or create a multi-threaded environment by the Win32 API. If you are only wanting a single-threaded timer, then Sleep()
MAY be an option, but it may be good to check that the sleep interval is a value > 0. For a multi-threaded timer though... I don't know the answer to that at this point
![Smile Smile](https://sinister.ly/images/smilies/set/smile.png)
You may get undesirable effects when using sleep if there is not some fail-safe mechanism behind it to validate intervals between times when these sleeped threads can actually do stuff again.
The way I see it, Sleep() shouldn't be used as the delay, but rather to yeild CPU time for something else so it's not being eaten up by a continuous loop, and checking whether the interval is just right (the tempting method of creating a timer with clock()). Sleep() in combination with clock() is probably the way. However, with clock() validating the intervals, and you should be able to get some precision out of that.
You'll be wanting to take more precaution when dealing with Sleep() by multiple threads though and especially if the priority of those threads is changeable, for the reasons behind what was mentioned above. I posted that your method wasn't the greatest for a few reasons, but another obvious one is for precision. Sleep() doesn't guarantee that the thread will run immediately after again, even in it's ready state, so this can't be used for a very exact measurement as an interval, as this reduces the precision of the timer itself.
If you don't care about using the Win32 API, you've got a few readily available functions already:
-
http://msdn.microsoft.com/en-us/library/...85%29.aspx
-
http://msdn.microsoft.com/en-us/library/...85%29.aspx
- etc...
For a good timer, portability seems to be tough. So if you're developing for Windows here, I see nothing wrong with using the Win32 API. Otherwise, for Linux, there are some nice timer libraries out there that I found as well. I think one was called GTimer?
Good luck
![Smile Smile](https://sinister.ly/images/smilies/set/smile.png)
If you find a good solution, be sure to share it, as I would also be interested in seeing what a C programmer would do for such a thing. I'm more of a C++ person. @"Psycho_Coder" is the C guy.