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)

烟雨青岚
Release: 2020-07-08 11:25:22
forward
5873 people have browsed it

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);  
Copy after login

Note:

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

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;  
} 
Copy after login

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);
Copy after login

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);  
Copy after login

Example:

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

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template