Non-blocking sleep timer in C


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.



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 <a href="http://www.adrianxw.dk/SoftwareSite/Threads/Threads1.html" rel="nofollow">tutorial</a>, that might get you started with this.


The Win32 API has <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906%28v=vs.85%29.aspx" rel="nofollow">SetTimer()</a>. There are <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms644901%28v=vs.85%29.aspx#creating_timer" rel="nofollow">code examples</a> linked from that page.


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 <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms682453%28v=vs.85%29.aspx" rel="nofollow">create a separate thread</a> 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.


On Windows you have following options:


SetTimer - for GUI applications. Resolution around 15 milliseconds.<br /><a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906(v=vs.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906(v=vs.85).aspx</a>

</li> <li>

WaitableTimer - for non GUI application. You can wait for expiration of this timer using WaitFor.... kind of functions or timer could run callback function. Resolution around 15 milliseconds. <a href="http://msdn.microsoft.com/en-us/library/ms687008(v=VS.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms687008(v=VS.85).aspx</a><br /><a href="http://msdn.microsoft.com/en-us/library/ms686898(v=VS.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms686898(v=VS.85).aspx</a>

</li> <li>

Timer Queues - use this technique if you need to handle many timers. I think this timer could be more accurate than 15 ms. <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms687003(v=vs.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/windows/desktop/ms687003(v=vs.85).aspx</a>

</li> <li>

Multimedia Timer - most accurate timer with 1 ms resolution. Unfortunately there is limit of those timers. I think 16 is max number. <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/dd757664(v=vs.85).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/windows/desktop/dd757664(v=vs.85).aspx</a>

</li> </ul>

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.


Does the solution in this question, answer yours?

<a href="https://stackoverflow.com/questions/30126609/timer-to-implement-a-time-out-function/30128378#30128378" rel="nofollow">Timer to implement a time-out function</a>

Regards, Toonie.


  • CFNetwork SSLHandshake failed (-9806) & (-9800) & (-9830)
  • Submission of new app with iAds
  • Injecting service into a mixin Ember2.3+
  • TextBox AutoCompleteStringCollection Suggest
  • How to sort a same column both in asc order and desc order
  • React Router and Arbitrary Query Params: Page Refreshes Unintentionally on Load?
  • remove unicode characters but keep all special and English characters with preg_replace
  • Why isn't my “Fizz Buzz” test in R working?
  • Python PIL to extract number from image
  • Cross platform UI spacing/padding
  • mave 3.2 not able to access local nexus instance return 502 code
  • CodeIgniter URI Parameter is partially bypassing an “if” statement
  • opencv display image without x server
  • Find group of records that match multiple values
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • Sequential (transactional) API calls in angular 4 with state management
  • Custom Tabgroup Appcelerator
  • Use of this Javascript
  • $wpdb not working in file of WordPress plugin
  • Database structure design with variable amounts of fields
  • C++ Partial template specialization - design simplification
  • Highlight one bar in a series in highcharts?
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Javascript Callbacks with Object constructor
  • htaccess rewriting URLs with multiple forward slashes
  • Javascript + PHP Encryption with pidCrypt
  • Display Images one by one with next and previous functionality
  • Web-crawler for facebook in python
  • How to get next/previous record number?
  • XCode can't find symbols for a specific iOS library/framework project
  • Arrays break string types in Julia
  • Matrix multiplication with MKL
  • A cron job substitute?
  • Python: how to group similar lists together in a list of lists?
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Qt: Run a script BEFORE make
  • How can I remove ASP.NET Designer.cs files?
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal