Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

  // 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.

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().

Please give me the reason for this.

like image 977
Aneesh Narayanan Avatar asked Mar 22 '12 15:03

Aneesh Narayanan


People also ask

What is the use of sleep function?

Description: The sleep () function causes the program or the process in which it is called, to suspend its execution temporarily for a period of time in seconds specified by the function parameter. Execution is suspended until the requested time is elapsed or a signal or an interrupt is delivered to the function.

What does sleep () do in Python?

Python time sleep function is used to add delay in the execution of a program. We can use python sleep function to halt the execution of the program for given time in seconds.

What is sleep () in C?

The function sleep gives a simple way to make the program wait for a short interval. If your program doesn't use signals (except to terminate), then you can expect sleep to wait reliably throughout the specified interval.

What is sleep method in Java?

sleep() method can be used to pause the execution of current thread for specified time in milliseconds. The argument value for milliseconds can't be negative, else it throws IllegalArgumentException .


2 Answers

According to 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.

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.

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.

like image 179
SirDarius Avatar answered Sep 24 '22 06:09

SirDarius


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.

like image 21
manasij7479 Avatar answered Sep 25 '22 06:09

manasij7479