简体   繁体   English

Sleep()函数用法

[英]Sleep() function usage

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. 这是一个检查Sleep()函数功能的示例pgm。这是一个演示,因为我在我的app开发中使用了这个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. 在这段代码中,我在睡眠功能之前和之后使用clock()来计算时间。 Since iam using sleep(30000), the time diff would be atleast 30000. 由于iam使用sleep(30000),时间差异至少为30000。

I have run this prgm many times. 我已多次运行此prgm。 and printed output as 30000, 30001, 30002. These are ok. 打印输出为30000,30001,30002。这些都可以。 But some times i am getting values like 29999 and 29997.How this possible, since i put 30000 sleep b/w the clock(). 但有时我得到像29999和29997这样的值。这怎么可能,因为我把30000睡眠时钟()。

Please give me the reason for this. 请告诉我原因。

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. 如果dwMilliseconds小于系统时钟的分辨率,则线程可能会睡眠时间少于指定的时间长度。 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大于一个tick但小于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 timers that may better fit your needs. 还有高分辨率计时器可以更好地满足您的需求。

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. 操作系统必须安排并运行许多其他进程,因此唤醒您的进程可能与您想要睡眠的确切时间不匹配。

The clock() function tells how much processor time the calling process has used. clock()函数告诉调用进程使用了​​多少处理器时间。

You may replace the use of clock() by the function GetSystemTimeAsFileTime in order to measure the time more accurately. 您可以使用函数GetSystemTimeAsFileTime替换clock()的使用,以便更准确地测量时间。

Also you may try to use timeBeginPeriod with wPeriodMin returned by a call to timeGetDevCaps in 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 更多信息abount睡眠可以在这里找到

#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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM