Sleep Function in C++

Last Updated : 1 Jun, 2026

The sleep() function allows a program to pause its execution for a specified amount of time. It is commonly used when a temporary delay is needed between operations.

  • Suspends the execution of the current thread for a specified duration.
  • Useful for creating delays and controlling execution flow.
  • Available through platform-specific libraries such as <unistd.h> and <windows.h>.
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:

Explanation

  • The program first displays the messages "Join the Line" and "Wait for 5 seconds" on the screen.
  • The statement sleep(5) suspends the execution of the program for 5 seconds.
  • During this pause, the operating system can continue executing other processes and threads normally.
  • After the 5-second delay completes, the program resumes execution from the next statement.
  • Finally, the message "It's your time buy ticket" is printed, demonstrating how sleep() can be used to introduce delays between operations.

Syntax

sleep( time_period );    // time_period in seconds

Parameter:  time_period - Specifies the sleep duration in seconds

Return Type:  

  • Returns 0 if the sleep duration completes successfully
  • If interrupted by a signal, it returns the remaining sleep time

Note:

  • On Linux/UNIX systems, use sleep(seconds) from <unistd.h>.
  • On Windows systems, use Sleep(milliseconds) from <windows.h>.
  • Sleep(5000) and sleep(5) both introduce a delay of 5 seconds.

Similar Functions Like sleep in C++

C++ and operating-system libraries provide alternative functions that can be used to pause program execution with different levels of precision.

usleep()

The usleep() function is similar to sleep(), but it allows the delay duration to be specified in microseconds, making it suitable for shorter and more precise delays.

  • Defined in the <unistd.h> header file
  • Accepts the sleep duration in microseconds
  • Provides finer control over delays compared to sleep()
  • Commonly used when delays shorter than one second are required

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:

  • Returns 0 on successful execution.
  • Returns -1 if an error occurs.
C++
#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:

sleep_for()

The sleep_for() function suspends the execution of the current thread for a specified duration. Unlike sleep(), it is part of the C++ Standard Library and supports different time units such as seconds, milliseconds, and microseconds through the <chrono> library.

  • Defined in the <thread> header file
  • Uses duration types from the <chrono> library
  • Supports multiple time units such as seconds, milliseconds, and microseconds
  • Portable and recommended for modern C++ programs
  • May sleep for longer than the requested duration due to thread scheduling or resource contention

Syntax:

std::this_thread::sleep_for(std::chrono::time_duration(time_period));

Parameter: time_period - Specifies the duration for which the current thread should be suspended.

Return Type: sleep_for() has a return type of void. It does not return any value after the specified duration expires.

C++
#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:

sleep_until()

The sleep_until() function suspends the execution of the current thread until a specified point in time is reached. Unlike sleep_for(), which waits for a duration, sleep_until() waits until a particular time instant. It is provided by the C++ Standard Library through the <thread> header.

  • Defined in the <thread> header file
  • Suspends the current thread until a specified time point
  • Uses time points from the <chrono> library
  • Useful when a task must resume at a specific time

Syntax:

std::this_thread::sleep_until(awake_time);

Parameter:  awake_time - A std::chrono::time_point object representing the time at which the thread should resume execution.

Return Type

  • sleep_until() has a return type of void.
  • It does not return any value after the thread wakes up.
C++
#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:

Comment