Home >Backend Development >C#.Net Tutorial >Take you to understand the Sleep function in C language (with code)

Take you to understand the Sleep function in C language (with code)

烟雨青岚
烟雨青岚forward
2020-07-08 11:25:225882browse

Take you to understand the Sleep function in C language (with code)

Sleep function:

Function: Execution is suspended for a period of time

Usage:

unsigned sleep(unsigned seconds);  

Note:

In VC, use the header file #include abf42a29c589d8b0c4a13904441a279e. Under Linux, in the gcc compiler, the header file used differs depending on the gcc version# include f55648144b4a1c5cce7ad2f6519be0f3

In VC, the first English character in Sleep is capital "S", do not capitalize it under Linux, in standard C it is sleep, do not capitalize it, simple It is said that VC uses Sleep, and everything else uses sleep

In VC, the unit in Sleep() is in milliseconds, so if you want the function to stay for 1 second, it should be Sleep(1000); Under Linux, the unit in sleep() is seconds, not milliseconds.

Example:

#include <windows.h>  
int main() {  
  int a;  
  a=1000;  
  Sleep(a);  
  return 0;  
} 

usleep function:

Function: The usleep function suspends the process for a period of time, the unit is microsecond us ( millionths of a second).

Syntax:

void usleep(int micro_seconds);

Return value: None

Note: This function does not work in Windows operating system.

usleep() is similar to sleep(), used to delay suspended processes. The process is suspended and placed on the reday queue. But in general, when the delay time is on the order of seconds, use the sleep() function as much as possible. And this function has been deprecated, nanosleep can be used.

If the delay time is tens of milliseconds or less, use the usleep() function if possible. This way you can make optimal use of CPU time.

delay function:

Function: Pause the execution of the program for a period of time, the unit is milliseconds ms (one thousandth of a second)

Usage:

void delay(unsigned milliseconds);  

Example:

#include<dos.h>  
int main(void)  {  
    sound(440);  
    delay(500);   
    nosound();  
    return 0;  
}

delay() is a loop waiting, the process is still running, occupying the processor.

Sleep() is different, it will be suspended and give up the processor to other processes.

Thank you everyone for reading, I hope you will benefit a lot.

This article is reproduced from: https://blog.csdn.net/u011630575/article/details/45567599

Recommended tutorial: "C Language"

The above is the detailed content of Take you to understand the Sleep function in C language (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete