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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:16:32  来源:igfitidea点击:

Non-blocking sleep timer in C

cwindowswinapitimer

提问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 上,您有以下选项:

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.

在这种情况下,主线程不会被阻塞,可以继续工作。