C++ sleep() Function
![C++ Course: Learn the Essentials](https://www.scaler.com/topics/images/instructor-prateek.webp)
Overview
The sleep() function in C++ is an inbuilt function that is used to sleep the particular thread of the C++ that is running in the particular program.
The sleep() function is mainly used when we are multiple threads trying to access the same resource simultaneously.
The C++ sleep function is used to sleep the particular thread of the C++ program during the multithread environment for a particular interval of time.
Syntax of sleep() Function
In windows Environment:
The windows. h header file contains the sleep() method that can be used in windows OS.
Syntax:
In Unix Environment
whereas if we want to use the sleep() method for the Linux environment, the unistd.h header file contains the sleep() method.
The syntax of the Sleep function in windows and UNIX environments is given below.
Syntax:
Let's understand the parameters of the Sleep function in C++.
Parameters of C++ sleep() Function
The C++ Sleep function takes one parameter i.e the unsigned int that is used to denote the interval for which the current thread needs to sleep.
Parameter:
Return Value of C++ Sleep Function
The C++ sleep function returns a value i.e 0 when the sleep time of the particular thread is completed.
Exceptions of C++ Sleep Function
The C++ Sleep() function doesn't raise any exceptions.
How Does the C++ Sleep Function Work?
Let's understand how the C++ sleep function works in C++.
The OS is responsible for the executions of all threads that are running in the C++ Program, whenever the Sleep() method executes with the definite time interval that is provided by the user. The Sleep() method internally calls the system call of the OS that is responsible to move the current thread in the sleep state for a particular interval of time.
Uses of C++ Sleep Function
Let's understand the use cases of the sleep() function in C++
- The Sleep() method is mainly used when multiple threads trying to access the same resource.
- The second use case of the Sleep() method in C++ is to make the executions of the multithread environment synchronously.
Examples of C++ Sleep Function
Let's understand the Sleep() function in C++ using an example. In the below example, we are suspending the current thread in the window's environment for 3 seconds.
Output:
FAQs
Q: Which header file contains the Sleep() function in C++?
A: he windows.h and unistd.h header files provide the Sleep() function in C++.
Q: Can the Sleep() function accepts the negative value?
A: Yes, the Sleep() function accept the negative values.
Conclusion
- The Sleep() method is used to sleep the particular thread in the C++ Program.
- The Sleep() function takes one parameter that is used to denote the interval for which the current thread needs to sleep.
- The Sleep() in an inbuilt method in C++.
- The Sleep() methods can be used in Windows as well as Linux environments.
- The Sleep() can accept the negative values.