C++ Sleep() 函数用法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9825135/
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
Sleep() function usage
提问by Aneesh Narayanan
This is a sample pgm to check the functionality of Sleep() function.This is a demo only since iam using this sleep() and clock() functions in my app developement.
这是一个示例 pgm,用于检查 Sleep() 函数的功能。这只是一个演示,因为我在我的应用程序开发中使用了 sleep() 和 clock() 函数。
// TestTicks.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include <Windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
int i, i2;
i = clock();
//std::cout<<" \nTime before Sleep() : "<<i;
Sleep(30000);
i2 = clock();
//std::cout<<" \nTime After Sleep() : "<<i2;
std::cout<<"\n Diff : "<<i2 -i;
getchar();
return 0;
}
in this code i am calculating the time using clock() before and after the sleep function. Since iam using sleep(30000), the time diff would be atleast 30000.
在这段代码中,我在 sleep 函数之前和之后使用 clock() 计算时间。由于我使用 sleep(30000),因此时间差异至少为 30000。
I have run this prgm many times. and printed output as 30000, 30001, 30002. These are ok. But some times i am getting values like 29999 and 29997.How this possible, since i put 30000 sleep b/w the clock().
我已经多次运行这个程序。并打印输出为 30000、30001、30002。这些都可以。但有时我会得到像 29999 和 29997 这样的值。这怎么可能,因为我把 30000 睡眠 b/w 时钟()。
Please give me the reason for this.
请告诉我这样做的原因。
采纳答案by SirDarius
According to http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v=vs.85).aspx:
根据http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v=vs.85).aspx:
The system clock "ticks" at a constant rate. If dwMilliseconds is less than the resolution of the system clock, the thread may sleep for less than the specified length of time. If dwMilliseconds is greater than one tick but less than two, the wait can be anywhere between one and two ticks, and so on.
系统时钟以恒定速率“滴答作响”。如果 dwMilliseconds 小于系统时钟的分辨率,线程可能会休眠少于指定的时间长度。如果 dwMilliseconds 大于 1 滴答但小于 2,则等待时间可以在一到两个滴答之间,依此类推。
It just means that the Sleep function will never sleep exactly for the amount of time given, but as close as possible given the resolution of the scheduler.
这只是意味着 Sleep 函数永远不会在给定的时间内完全休眠,而是在调度程序的分辨率下尽可能接近。
The same page gives you a method to increase the timer resolution if you really need it.
如果您确实需要,同一页面为您提供了一种增加计时器分辨率的方法。
There are also high resolution timersthat may better fit your needs.
还有可能更适合您的需求的高分辨率计时器。
回答by manasij7479
Unless you're using a realtime OS, that is very much expected.
除非您使用实时操作系统,否则这是非常值得期待的。
The Operating System has to schedule and run many other processes, so waking yours' up may not match the exact time you wanted to sleep.
操作系统必须安排和运行许多其他进程,因此唤醒您的进程可能与您想要睡觉的确切时间不匹配。
回答by Arno
The clock()function tells how much processor time the calling process has used.
该时钟()函数告诉多少处理器时间的调用进程已经习惯了。
You may replace the use of clock() by the function GetSystemTimeAsFileTimein order to measure the time more accurately.
您可以用函数GetSystemTimeAsFileTime替换 clock() 的使用, 以便更准确地测量时间。
Also you may try to use timeBeginPeriodwith wPeriodMin returned by a call to timeGetDevCapsin order to obtail maximum interrupt frequency.
你也可以尝试使用timeBeginPeriod与wPeriodMin通过返回到呼叫timeGetDevCaps以中断频率obtail最大。
In order to synchronize the sleeps with the system interrupt period, I'd also suggest to have a sleep(1) ahead of the first "time capture".
为了使睡眠与系统中断周期同步,我还建议在第一次“时间捕获”之前进行睡眠(1)。
By doing so, the "too shorts" will disappear.
通过这样做,“太短”将消失。
More information abount sleep can be found here
可以在此处找到有关睡眠的更多信息
回答by shabab alam
#include <iostream>
#include <time.h>
void wait(int seconds)
{
int endwait;
endwait = clock() + seconds * CLOCKS_PER_SEC ;
while (clock() < endwait){}
}
int main()
{
wait(2);
cout<<"2 seconds have passed";
}