Updated March 30, 2023
Introduction to C++ thread sleep
Whenever there is a necessity to suspend the execution of a thread or a process for a specified period of time temporarily, then we make use of the function called sleep() function in C++, which takes the time in seconds as a parameter and it is the duration for which the execution of the thread or the process must be suspended, and this suspension of the thread or process continues until the specified time is completed or it can be interrupted by sending any interrupts to the function and the header windows.h must be included in the case of windows and the header unistd.h must be included in the case of Linux to make use of the sleep() function in our program.
Syntax of sleep() function in C++:
Sleep(time_in_milliseconds)
Where time_in_milliseconds represents the time duration for which the given thread or process must be suspended.
Working of sleep() Function in C++
- Whenever there is a necessity to temporarily suspend the execution of a thread or a process for a specified period of time, we use the sleep() function in C++.
- The sleep() function takes the time in seconds as a parameter, and it is the duration for which the execution of the thread or the process must be suspended.
- The suspension of the thread or process continues until the specified time is completed, or it can be interrupted by sending any interrupts to the function.
- The header windows.h must be included in the case of windows and the header unistd.h must be included in the case of Linux to make use of the sleep() function in our program.
Examples of C++ thread sleep
Given below are the examples of C++ thread sleep:
Example #1
C++ program to demonstrate sleep() function that suspends the first cout statement for a specified amount of time before printing the next cout statement.
Code:
//the headers iostream and unistd are included to be able to make use of cout and cin statements and sleep() function
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
//The first cout statement is executed
cout<<"Welcome"<<"\n";
//Then the sleep() function is called before executing the next cout statement
sleep(10);
//this cout statement is executed after the sleep function is executed for the speicifed amount of time
cout<<"to C++";
cout<<endl;
return 0;
}
Output:
In the above program, the headers iostream and unistd are included to be able to make use of cout and cin statements and sleep() function. Then the first cout statement is executed. Then the sleep() function is called before executing the next cout statement. Then, the next cout statement is executed after the sleep function is executed for a specified time.
Example #2
C++ program to demonstrate sleep() function that suspends the first cout statement for a specified amount of time before printing the next cout statement.
Code:
//the headers iostream and unistd are included to be able to make use of cout and cin statements and sleep() function
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
//The first cout statement is executed
cout<<"Learning"<<"\n";
//Then the sleep() function is called before executing the next cout statement
sleep(10);
//this cout statement is executed after the sleep function is executed for the speicifed amount of time
cout<<"is fun";
cout<<endl;
return 0;
}
Output:
In the above program, the headers iostream and unistd are included to be able to make use of cout and cin statements and sleep() function. Then the first cout statement is executed. Then the sleep() function is called before executing the next cout statement. Then, the next cout statement is executed after the sleep function is executed for a specified time.
Example #3
C++ program to demonstrate sleep() function that suspends the first cout statement for a specified amount of time before printing the next cout statement.
Code:
//the headers iostream and unistd are included to be able to make use of cout and cin statements and sleep() function
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
//The first cout statement is executed
cout<<"Learning"<<"\n";
//Then the sleep() function is called before executing the next cout statement
sleep(10);
//this cout statement is executed after the sleep function is executed for the speicifed amount of time
cout<<"sleep function";
cout<<endl;
return 0;
}
Output:
In the above program, the headers iostream and unistd are included to be able to make use of cout and cin statements and sleep() function. Then the first cout statement is executed. Then the sleep() function is called before executing the next cout statement. Then, the next cout statement is executed after the sleep function is executed for a specified time.
Example #4
C++ program to demonstrate sleep() function that suspends the first cout statement for a specified amount of time before printing the next cout statement.
Code:
//the headers iostream and unistd are included to be able to make use of cout and cin statements and sleep() function
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
//The first cout statement is executed
cout<<"I love"<<"\n";
//Then the sleep() function is called before executing the next cout statement
sleep(10);
//this cout statement is executed after the sleep function is executed for the speicifed amount of time
cout<<"my India";
cout<<endl;
return 0;
}
Output:
In the above program, the the headers iostream and unistd are included to be able to make use of cout and cin statements and sleep() function. Then the first cout statement is executed. Then the sleep() function is called before executing the next cout statement. Then, the next cout statement is executed after the sleep function is executed for a specified time.
Recommended Articles
This is a guide to C++ thread sleep. Here we discuss the introduction, working of sleep() function in C++ and examples, respectively. You may also have a look at the following articles to learn more –