Sleep Function in C++
Last Updated :
12 Oct, 2022
C++ provides the functionality of delay or inactive state 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. It can be implemented using 2 libraries according to the operating system being used:
#include<windows.h> // for windows
#include<unistd.h> // for linux
Sleep 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++
#include <iostream>
#include <windows.h>
#include <unistd.h>
using namespace std;
int main()
{
cout << "Join the Line:\n" ;
cout << "Wait for 5 seconds\n" ;
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++
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
cout << "Take your Position\n" ;
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++
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
int main()
{
cout << "Thread is running\n" ;
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++
#include <chrono>
#include <iostream>
#include <thread>
auto now()
{
return std::chrono::steady_clock::now();
}
auto awake_time()
{
using std::chrono::operator "" ms;
return now() + 2000ms;
}
int main()
{
std::cout << "Starting the operation .....\n" <<
std::flush;
const auto start{ now() };
std::this_thread::sleep_until(awake_time());
std::chrono::duration< double , std::milli> elapsed{
now() - start
};
std::cout << "Waited for : " <<
elapsed.count() << " ms\n" ;
}
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...