用C实现延时功能 [英] Implementing time delay function in C

查看:126
本文介绍了用C实现延时功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现使用空循环延时功能。但是,完成一个循环所需的时间量一度是编译器和机器相关。我希望我的程序来确定自身的时间和延迟指定的时间计划。谁能给我任何想法如何做到这一点?
N. B.有其暂停系统指定毫秒名为延迟功能()。是否有可能中止该系统不使用此功能?

I want to implement a delay function using null loops. But the amount of time needed to complete a loop once is compiler and machine dependant. I want my program to determine the time on its own and delay the program for the specified amount of time. Can anyone give me any idea how to do this? N. B. There is a function named delay() which suspends the system for the specified milliseconds. Is it possible to suspend the system without using this function?

推荐答案

首先,你应该的从不坐在一个循环无所事事。它不仅浪费能源(因为它让你的CPU 100%忙着数你的循环计数器) - 在多任务系统,这也降低了整个系统的性能,因为你的过程中越来越的时间片的所有时间显示为它的 的找点事干。

First of all, you should never sit in a loop doing nothing. Not only does it waste energy (as it keeps your CPU 100% busy counting your loop counter) -- in a multitasking system it also decreases the whole system performance, because your process is getting time slices all the time as it appears to be doing something.

第二点是......我不知道任何延迟()的功能。这也不是标准C.事实上,直到C11,没有标准可言了这样的事情。

Next point is ... I don't know of any delay() function. This is not standard C. In fact, until C11, there was no standard at all for things like this.

POSIX救援,有 usleep(3) (德precated)和 了nanosleep (2) 。如果你是一个POSIX兼容的系统上,你会没事的那些。他们的的(手段,你的操作系统的调度知道他们什么都没有做,并调度它们只是通话结束后),这样你就不会浪费CPU资源。

POSIX to the rescue, there is usleep(3) (deprecated) and nanosleep(2). If you're on a POSIX-compliant system, you'll be fine with those. They block (means, the scheduler of your OS knows they have nothing to do and schedules them only after the end of the call), so you don't waste CPU power.

如果您使用的是Windows,为code直接的延迟,你只有的 睡眠() 。注意,这个函数接受毫秒,但通常只有约15毫秒一个precision。通常不够好,但并非总是如此。如果您需要在Windows更好的precision,你可以要求更多的计时器中断使用的 timeBeginPeriod() ... timeBeginPeriod(1); 将请求一个定时器中断每毫秒。不要忘记调用 timeEndPeriod() ,提供的相同的值的,只要你不需要precision更多,因为更多的定时器来中断与成本:他们保持系统繁忙,因而浪费了更多的能量

If you're on windows, for a direct delay in code, you only have Sleep(). Note that THIS function takes milliseconds, but has normally only a precision around 15ms. Often good enough, but not always. If you need better precision on windows, you can request more timer interrupts using timeBeginPeriod() ... timeBeginPeriod(1); will request a timer interrupt each millisecond. Don't forget calling timeEndPeriod() with the same value as soon as you don't need the precision any more, because more timer interrupts come with a cost: they keep the system busy, thus wasting more energy.

我最近有一个有点类似的问题,发展中的小游戏,我需要10ms间隔不变蜱,这就是我想出的为POSIX兼容的系统和的对于Windows 。在这code中的 ticker_wait()函数只是暂停,直到下一个刻度,也许这是有益的,如果你的初衷是一些时间问题。

I had a somewhat similar problem developing a little game recently, I needed constant ticks in 10ms intervals, this is what I came up with for POSIX-compliant systems and for windows. The ticker_wait() function in this code just suspends until the next tick, maybe this is helpful if your original intent was some timing issue.

这篇关于用C实现延时功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆