Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


Timer in C filter_list
Author
Message
RE: Timer in C #11
You can't stop it from outside the function if the function is running unless you're using more than one thread to do that.

However, if you could do that, my code should allow you to do so by just changing the value of timerStatus to whatever else than 1, I hope.
My Bitcoin address: 1AtxVsSSG2Z8JfjNy9KNFDUN6haeKr7LiP
Give me money by visiting www.google.com here: http://coin-ads.com/6Ol83U

If you want a Bitcoin URL shortener/advertiser, please, use this referral: http://coin-ads.com/register.php?refid=noize

Reply

RE: Timer in C #12
(06-16-2013, 05:52 PM)Psycho_Coder Wrote: Your object was not clear to me that what you actually want to make or do.
C has a time.h library for timing functions have a look :- http://www.cplusplus.com/reference/ctime/

Also remember it is not useful to use Sleep() in C as it is not thread safe. Also its is not apart of standard C library.
It depends on the compiler. In some compilers it is there is dos.h opr in some in process.h or in windows.h it depends also there are some other functions like kill().

If you want to create stop watch or something then learn to use the POSIX library

Thank you,
Sincerely,
Psycho_Coder

The Sleep function is thread safe.. :huh: If it wasn't there would be no point to it. The whole purpose of Sleep() is to yield CPU time. There is a special case as well in which you can yield CPU time to a high priority thread using Sleep(0) for instance, in which the thread will relinquish the remainder of its time slice but remain ready assuming there is a higher priority thread, otherwise it won't yield to anything but itself, thus this doesn't apply if the thread is not the one with the highest priority -- or if you sleep with a value > 0 because then the functionality of calling this function is different. A ready thread is not guaranteed to run right away though so this is where it's misuse comes into place (aside from that special case of Sleep(0) anyways); using it for CPU time yielding to managed event based execution. That sleep time doesn't mean anything for when the thread will start again.

I don't think you meant to say it is not "thread safe"... But perhaps that it is not "safe"? Unless you really know what you're doing with it, I usually see it being misused though.

And... For that very reason, this is horrible:
Code:
int Timer(int interval){

if(timerStatus == 1)
{
    Sleep(interval);
    //Proccess goes here
    printf("%s\n","Test");
    Timer(interval);
}

If this is used in a multi-threaded application, or as a timer that handles multiple threads (perhaps a threadpool), you're in for a disasterous outcome. And you'd also better hope that someone doesn't try to choose an interval of 0.

I don't know everything about C, but that sleep function is something I'm fairly familiar with. As far as I have read though clock() is part of the ANSI Standard, and headers like dos.h don't exist in standard C. So with that being said, it may be tempting to make a while loop checking clock() differences, but it would appear then that you don't care about CPU usage... And using Sleep() (perhaps in combination with the loop to avoid tons of iterations in a short period of time), well that is explained above in the issues with Sleep().

Feel free to prove I'm wrong here, but I don't think in that link you've provided it says that Sleep() is not "thread safe." :ok:

Here is a good link for a bit of information: http://cboard.cprogramming.com/c-program...ers-c.html (NOTE: The final code the guy came up with is still not quite a diamond... But, the information posted by others is kinda useful.)

@OP- Are you wanting callbacks here? Because the way you have it set up now, that timer is not really manipulable.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply

RE: Timer in C #13
Sir @ArkPhaze I have read something about it being not thread safe on stacl overflow. But your explanation is best and thanks for correcting my concepts.

EDIT: I am not knowledgable enough to prove you wrong because when you reply to some post you know what you are saying and you have read about it well. I will be like you someday :happy:
I think its better to learn from documentation than books as books doesn't covers every detail.

Thank You
Sincerely
Psycho_Coder
[Image: OilyCostlyEwe.gif]

Reply

RE: Timer in C #14
(06-17-2013, 05:13 AM)ArkPhaze Wrote:
(06-16-2013, 05:52 PM)Psycho_Coder Wrote: Your object was not clear to me that what you actually want to make or do.
C has a time.h library for timing functions have a look :- http://www.cplusplus.com/reference/ctime/

Also remember it is not useful to use Sleep() in C as it is not thread safe. Also its is not apart of standard C library.
It depends on the compiler. In some compilers it is there is dos.h opr in some in process.h or in windows.h it depends also there are some other functions like kill().

If you want to create stop watch or something then learn to use the POSIX library

Thank you,
Sincerely,
Psycho_Coder

The Sleep function is thread safe.. :huh: If it wasn't there would be no point to it. The whole purpose of Sleep() is to yield CPU time. There is a special case as well in which you can yield CPU time to a high priority thread using Sleep(0) for instance, in which the thread will relinquish the remainder of its time slice but remain ready assuming there is a higher priority thread, otherwise it won't yield to anything but itself, thus this doesn't apply if the thread is not the one with the highest priority -- or if you sleep with a value > 0 because then the functionality of calling this function is different. A ready thread is not guaranteed to run right away though so this is where it's misuse comes into place (aside from that special case of Sleep(0) anyways); using it for CPU time yielding to managed event based execution. That sleep time doesn't mean anything for when the thread will start again.

I don't think you meant to say it is not "thread safe"... But perhaps that it is not "safe"? Unless you really know what you're doing with it, I usually see it being misused though.

And... For that very reason, this is horrible:
Code:
int Timer(int interval){

if(timerStatus == 1)
{
    Sleep(interval);
    //Proccess goes here
    printf("%s\n","Test");
    Timer(interval);
}

If this is used in a multi-threaded application, or as a timer that handles multiple threads (perhaps a threadpool), you're in for a disasterous outcome. And you'd also better hope that someone doesn't try to choose an interval of 0.

I don't know everything about C, but that sleep function is something I'm fairly familiar with. As far as I have read though clock() is part of the ANSI Standard, and headers like dos.h don't exist in standard C. So with that being said, it may be tempting to make a while loop checking clock() differences, but it would appear then that you don't care about CPU usage... And using Sleep() (perhaps in combination with the loop to avoid tons of iterations in a short period of time), well that is explained above in the issues with Sleep().

Feel free to prove I'm wrong here, but I don't think in that link you've provided it says that Sleep() is not "thread safe." :ok:

Here is a good link for a bit of information: http://cboard.cprogramming.com/c-program...ers-c.html (NOTE: The final code the guy came up with is still not quite a diamond... But, the information posted by others is kinda useful.)

@OP- Are you wanting callbacks here? Because the way you have it set up now, that timer is not really manipulable.

What should I do then?
Fuck You.

Reply

RE: Timer in C #15
(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". Smile

(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 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 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.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply







Users browsing this thread: 1 Guest(s)