C++ provides the functionality of delay or inactive state of the program with the help of the operating system for a specific period of time. Other CPU operations will function adequately but the sleep() function in C++ will sleep the present executable for the specified time by the thread.
The sleep() function is not a standard libary function, it is defined inside the <unistd.h> for UNIX/Linux operating systems.
Windows alternative for sleep() function is Sleep() which is defined inside the <windows.h> header file.
The sleep() function can suspend execution for time_period where time_period is in seconds by default although we can change it to microseconds.
Syntax:
sleep( time_period ); // time_period in seconds
Parameter: time_period is in seconds it represents the sleep time taken.
Return Type: The return type of sleep function is an integer where if the function is successfully executed then the value returned will be 0, else minus the value of the time period returned.
Example:
C++
// C++ Program to show how to use
// sleep function
#include <iostream>
// Library effective with Windows
#include <windows.h>
// Library effective with Linux
#include <unistd.h>
using namespace std;
// Driver code
int main()
{
cout << "Join the Line:\n";
cout << "Wait for 5 seconds\n";
// sleep will schedule rest of
// activities after 5 seconds
sleep(5);
cout << "It's your time buy ticket";
}
Output:
Similar Functions Like sleep in C++
1. usleep(): This function is mostly similar to sleep but can only be used with <unistd.h> library.
Syntax:
usleep(time_period) // time_period in microseconds
Parameter: It takes time_period where time_period is by default in microseconds. 1 second = 10^6 microseconds.
Return Type: Integer where it returns 0 if successful, and (-1 or -exp) if the process failed.
Example:
C++
// C++ Program to show the
// use of usleep function
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
cout << "Take your Position\n";
// sleep for 5 seconds
cout << "Wait for 5 seconds\n";
usleep(5000000);
cout << "Run! Run!";
return 0;
}
Output:
2. sleep_for(): Schedules thread for the specified time. It acts like a delay just like sleep function. However, It is possible that threads take more time than the scheduled time due to scheduling activities or can be resource contention delays. Library used <thread>.
Syntax:
this_<thread_name>::sleep_for(chorno:: time_duration (time_period))
Parameter: time_period ( time for which thread is acquired )
Example:
C++
// C++ Program to demonstrate
// sleep_for()function
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
// Driver cpde
int main()
{
cout << "Thread is running\n";
// Thread delayed for 5 seconds
this_thread::sleep_for(chrono::milliseconds(5000));
cout << "Thread was acquired for 5 seconds\n";
return 0;
}
Output:
3. sleep_until(): Blocks the execution of a thread until the sleep_time is finished. However, even when sleep_time has been reached due to scheduling or resource contention delays it could take more time than sleep_time. Library used <thread>.
Syntax:
this_<thread_name>::sleep_until(awake_time)
Parameter: Sleep_time (same time for which thread is blocked for execution)
Example:
C++
// C++ Program to demonstrate
// sleep_until()
#include <chrono>
#include <iostream>
#include <thread>
// Functioning returning
// current time
auto now()
{
return std::chrono::steady_clock::now();
}
// Function calculating sleep time
// with 2000ms delay
auto awake_time()
{
using std::chrono::operator"" ms;
return now() + 2000ms;
}
// Driver code
int main()
{
std::cout << "Starting the operation .....\n" <<
std::flush;
// Calculating current time
const auto start{ now() };
// using the sleep_time to delay
// and calculating sleep time
// using awake_time function
std::this_thread::sleep_until(awake_time());
// storing time for printing
std::chrono::duration<double, std::milli> elapsed{
now() - start
};
// printing waiting time
std::cout << "Waited for : " <<
elapsed.count() << " ms\n";
}
Output:
Similar Reads
raise() function in C++
csignal header file declared the function raise() to handle a particular signal. Signal learns some unusual behavior in a program, and calls the signal handler. It is implemented to check if the default handler will get called or it will be ignored. Syntax: int raise ( int signal_ ) Parameter: The f
3 min read
std::function in C++
The std::function() in C++ is a function wrapper class which can store and call any function or a callable object. In this article, we will learn about std::function in C++ and how to use it in different cases.Table of ContentWhat is std::function in C++?Example of std::functionMember Functions of s
5 min read
std::function in C++
The std::function() in C++ is a function wrapper class which can store and call any function or a callable object. In this article, we will learn about std::function in C++ and how to use it in different cases.Table of ContentWhat is std::function in C++?Example of std::functionMember Functions of s
5 min read
log() Function in C++
The std::log() in C++ is a built-in function that is used to calculate the natural logarithm (base e) of a given number. The number can be of any data type i.e. int, double, float, long long. It is defined inside the <cmath> header file.In this article we will learn about how to use std::log()
1 min read
log() Function in C++
The std::log() in C++ is a built-in function that is used to calculate the natural logarithm (base e) of a given number. The number can be of any data type i.e. int, double, float, long long. It is defined inside the <cmath> header file.In this article we will learn about how to use std::log()
1 min read
log10() Function in C++
The std::log10() in C++ is a built-in function that is used to calculate the base-10 logarithm of a given number. It is defined inside <cmath> header file. In this article, we will learn about log10() in C++ and its behavior for different values.Example:C++// C++ Program to illustrate the use
2 min read
log10() Function in C++
The std::log10() in C++ is a built-in function that is used to calculate the base-10 logarithm of a given number. It is defined inside <cmath> header file. In this article, we will learn about log10() in C++ and its behavior for different values.Example:C++// C++ Program to illustrate the use
2 min read
localtime() function in C++
The localtime() function is defined in the ctime header file. The localtime() function converts the given time since epoch to calendar time which is expressed as local time. Syntax: tm* localtime(const time_t* time_ptr); Parameter: This function accepts a parameter time_ptr which represents the poin
1 min read
Function Pointer in C++
Prerequisites: Pointers in C++Function in C++ Pointers are symbolic representations of addresses. They enable programs to simulate call-by-reference as well as to create and manipulate dynamic data structures. Iterating over elements in arrays or other data structures is one of the main use of point
4 min read
clock() function in C
The clock() function in C returns the approximate processor time that is consumed by the program which is the number of clock ticks used by the program since the program started. The clock() time depends upon how the operating system allocates resources to the process that's why clock() time may be
2 min read