windows C中的非阻塞睡眠定时器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7771142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Non-blocking sleep timer in C
提问by luacoder
I'm looking for a good non-blocking sleep timer in C for windows.
我正在为 Windows 寻找一个很好的非阻塞睡眠定时器。
Currently I am using sleep(10);
which of course is a blocking timer.
目前我正在使用sleep(10);
这当然是一个阻塞计时器。
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.
此外,我希望它不消耗系统资源,就像我的睡眠定时器一样,它不使用我满意的任何 CPU 或系统资源。
So, what is the best non-blocking sleep timer I could use? And please also include an example of how to use it.
那么,我可以使用的最好的非阻塞睡眠定时器是什么?并请附上一个如何使用它的例子。
Thanks.
谢谢。
采纳答案by anijhaw
You dont need an API you need to change your design.
您不需要更改设计所需的 API。
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.
您可以有多个线程,一个是管理器线程,另一个是工作线程。每 10 秒,管理器线程将唤醒创建一个新的工作线程并进入睡眠状态。即使 Manager 处于睡眠状态,工作线程也会继续工作,并且您具有非阻塞效果。
I dont know how familar you are with threads, but here is a very very basic tutorial, that might get you started with this.
我不知道你对线程有多熟悉,但这里有一个非常非常基础的教程,它可能会让你开始这个。
回答by Zuljin
On Windows you have following options:
在 Windows 上,您有以下选项:
SetTimer - for GUI applications. Resolution around 15 milliseconds.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906(v=vs.85).aspxWaitableTimer - 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. http://msdn.microsoft.com/en-us/library/ms687008(v=VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms686898(v=VS.85).aspxTimer Queues - use this technique if you need to handle many timers. I think this timer could be more accurate than 15 ms. http://msdn.microsoft.com/en-us/library/windows/desktop/ms687003(v=vs.85).aspx
Multimedia Timer - most accurate timer with 1 ms resolution. Unfortunately there is limit of those timers. I think 16 is max number. http://msdn.microsoft.com/en-us/library/windows/desktop/dd757664(v=vs.85).aspx
SetTimer - 用于 GUI 应用程序。分辨率约为 15 毫秒。
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906(v=vs.85).aspxWaitableTimer - 用于非 GUI 应用程序。您可以使用 WaitFor... 等函数或计时器可以运行回调函数来等待此计时器到期。分辨率约为 15 毫秒。 http://msdn.microsoft.com/en-us/library/ms687008(v=VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms686898(v=VS.85) .aspx计时器队列 - 如果您需要处理多个计时器,请使用此技术。我认为这个计时器可能比 15 毫秒更准确。 http://msdn.microsoft.com/en-us/library/windows/desktop/ms687003(v=vs.85).aspx
多媒体计时器 - 分辨率为 1 毫秒的最准确计时器。不幸的是,这些计时器是有限的。我认为 16 是最大数量。 http://msdn.microsoft.com/en-us/library/windows/desktop/dd757664(v=vs.85).aspx
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.
您还应该记住,计时器回调函数将在与您的应用程序不同的线程下运行。因此,如果回调函数使用与主线程相同的数据,那么您需要保护它们(使用 Mutex、CriticalSection 等)以消除多个线程的同时访问。
回答by Toonie
Does the solution in this question, answer yours?
这个问题的解决方案是否能回答你的问题?
Timer to implement a time-out function
Regards, Toonie.
问候,托尼。
回答by Martin Stone
The Win32 API has SetTimer(). There are code exampleslinked from that page.
Win32 API 具有SetTimer()。有从该页面链接的代码示例。
回答by Mark Wilkins
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 threadwhose responsibility is to execute some function every 10 seconds. It could use Sleep( 10000 )
to block itself, then call the function, then sleep, etc.
根据 OP 最近对问题的评论,听起来好像目标是允许程序继续处理但能够每 10 秒调用一次函数。提供此功能的一种方法是创建一个单独的线程,其职责是每 10 秒执行一次函数。它可以Sleep( 10000 )
用来阻塞自己,然后调用函数,然后睡眠等。
The main thread would not be blocked in this case and could continue doing its work.
在这种情况下,主线程不会被阻塞,可以继续工作。