7

I'm looking for a good non-blocking sleep timer in C for windows.

Currently I am using sleep(10); which of course is a blocking timer.

Also I want it to consume no system resources, like my sleep timer it doesn't use any CPU or system resources which I am happy with.

So, what is the best non-blocking sleep timer I could use? And please also include an example of how to use it.

Thanks.

cpx
  • 17,009
  • 20
  • 87
  • 142
luacoder
  • 209
  • 2
  • 4
  • 9
  • 1
    What are you actually trying to accomplish? What are you waiting for? Could you make use of a named event (see: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682396%28v=vs.85%29.aspx) – Will Chesterfield Oct 14 '11 at 17:13
  • 2
    What is the overall result you want to achieve? Typically sleep is used for multithreaded applications to relinquish control. If you don't want it to block, then don't write sleep? – Matthew Oct 14 '11 at 17:15
  • 4
    So you want to block execution of your program... without blocking... huh? – Ed S. Oct 14 '11 at 17:16
  • 1
    The have been a few questions about windows timers here. [This one](http://stackoverflow.com/questions/2858422/are-timers-supported-by-windows-native-api), for instance. Are non of the options there good enough? – Eran Oct 14 '11 at 17:16
  • @Will Chesterfield, No I don't think so. I'm looking for something to replace the "sleep(10);" which blocks the program. But something simple so I can understand it. – luacoder Oct 14 '11 at 17:17
  • 1
    @luacoder - seems like we are having a communication problem. what is the exact effect you're looking to achieve? If you want to have something happen in the future, how about SetTimer ala http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906%28v=vs.85%29.aspx - but again, just stabbing in the dark as your requirements hard to understand. – Will Chesterfield Oct 14 '11 at 17:23
  • A non-blocking sleep is just an empty function! I think you need to be more precise what it is you're looking for. Do you want your program to continue running, and then 10 seconds later, some other function starts running? How do you want the program and the other function to coordinate their actions? Do you want the other function to run simultaneous with the main program? Do you want the main program suspended while the other function runs? Depending on what you want, the answers will vary. – Raymond Chen Oct 14 '11 at 17:26
  • 5
    Non-blocking sleep is an oxymoron – David Heffernan Oct 14 '11 at 17:34
  • 1
    Oh I'm sorry. Yea I want it to do something in the future each 10 second lets say. Yes I want to continue running and after 10 seconds execute a function. And being able to repeat the process each 10 seconds. While the program is fully functional, meaning not blocked I guess best to describe? Hence the term non-blocking sleep/timer or is that wrong way of describing it? I saw some topic describe it as such. Sorry if I was unclear. – luacoder Oct 14 '11 at 17:35

5 Answers5

6

You dont need an API you need to change your design.

A simple one is this.

You can have multiple threads, One is the Manager Thread and other are Worker Threads. At every 10 seconds the manager thread will wake up create a new worker thread and go to sleep. The worker threads will keep working even when the Manager is sleeping and this you have your non blocking effects.

I dont know how familar you are with threads, but here is a very very basic tutorial, that might get you started with this.

anijhaw
  • 8,954
  • 7
  • 35
  • 36
1

On Windows you have following options:

You should also remember that timer callback function will be run under different thread that your application. So if callback function use the same data as your main thread then you need to protect them (using Mutex, CriticalSection etc.) to eliminate simultaneous access by multiple threads.

Zuljin
  • 2,612
  • 17
  • 14
0

Does the solution in this question, answer yours?

Timer to implement a time-out function

Regards, Toonie.

Community
  • 1
  • 1
Toonie
  • 33
  • 5
0

The Win32 API has SetTimer(). There are code examples linked from that page.

Martin Stone
  • 12,682
  • 2
  • 39
  • 53
0

Based on the OP's recent comment to questions, it sounds as if the goal is to allow the program to continue processing but be able to call some function every 10 seconds. One way of providing this functionality would be to create a separate thread whose responsibility is to execute some function every 10 seconds. It could use Sleep( 10000 ) to block itself, then call the function, then sleep, etc.

The main thread would not be blocked in this case and could continue doing its work.

Mark Wilkins
  • 40,729
  • 5
  • 57
  • 110
  • Yea that looks good. But I don't have any threads at the moment in my app though. How do I implement this? I read something earlier about a System.Thread is that what you are referring too? Or something similar. – luacoder Oct 14 '11 at 17:49
  • If he's using the C runtime library he should use _beginthread(), not CreateThread(). – Carey Gregory Oct 14 '11 at 18:18