问题描述
我正在为 Windows 寻找一个很好的非阻塞睡眠定时器.
I'm looking for a good non-blocking sleep timer in C for windows.
目前我正在使用 sleep(10);
这当然是一个阻塞计时器.
Currently I am using sleep(10);
which of course is a blocking timer.
此外,我希望它不消耗系统资源,就像我的睡眠定时器一样,它不使用任何我满意的 CPU 或系统资源.
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.
谢谢.
推荐答案
您不需要更改设计所需的 API.
You dont need an API you need to change your design.
一个简单的就是这个.
你可以有多个线程,一个是管理线程,一个是工作线程.每 10 秒,管理器线程将唤醒创建一个新的工作线程并进入睡眠状态.即使 Manager 处于睡眠状态,工作线程也会继续工作,并且您具有非阻塞效果.
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.
这篇关于C中的非阻塞睡眠定时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!