C++ Sleep(), usleep(), sleep_for() Functions

Functions to delay execution of C++ Program

In this tutorial, we will learn how to delay the execution of code in C++ programs. The following functions are explained with examples below:

  • Sleep() function
  • sleep_for()
  • usleep()

What is Sleep function in C++

  • To delay the execution of C++ program, we can use the Sleep() function.
  • The Sleep() function is part of Windows.h header file, so you should use other functions for cross-platform compatibility.
  • The value provided in the Sleep() function is in milliseconds. i.e. 1000 = 1 second

Sleep_for – Other function for delay in execution of C++ program or thread

You may use other functions for cross-platform compatibility. One of the functions is:

sleep_for()

This is part of chrono library, so you have to include this in the header section.

Note: This function is only available from C++ 11 and onwards.

An example of C++ Sleep function for windows

In this example to explain how C++ Sleep function works, we have used a for loop.

The loop iterates five times and in each iteration, the Sleep function is executed to delay the execution by 1000 milliseconds (i.e. one second).

#include <iostream>
#include <Windows.h>
using namespace std;

int main() {

  for (int x = 1; x <= 5; x++) {

    cout << x << "\n";

    Sleep(1000); // value in millisecond

  }

  return 0;

}

Output:

CPP-Sleep

In the above output, you can see the execution time is around 5 seconds.

To show the difference, I just disabled the following in the above code i.e:

//    Sleep(1000); // value in millisecond
#include <iostream>

#include <Windows.h>

using namespace std;


int main() {

  for (int x = 1; x <= 5; x++) {

    cout << x << "\n";

//    Sleep(1000); // value in millisecond

  }

  return 0;

}

Output:

CPP-Sleep-disabled

You can see that it only took 0.008 seconds to execute the above program.

Copy/Paste the above programs and execute them in your editor to see this working.

An example of using sleep_for() function for Linux (cross-platform)

We are again using the for loop to iterate ten times to demonstrate the sleep_for function in C++.

To show the difference we will run the program with and without the sleep_for() and you can notice the execution time difference in both programs.

C++ program with sleep_for() function:

#include <iostream>

#include <chrono>

#include <thread>

using namespace std;


int main() {

  for (int x = 1; x <= 10; x++) {

    cout <<"x = "<< x << "\n";

    //Using sleep_for for 0.5 second delay

    std::this_thread::sleep_for(500ms);

  }

  return 0;




}

Output:

CPP-Sleep_for

It took 5.007 seconds in our compiler as we executed this code.

As value is given as 500ms.

C++ program without sleep_for() function:

#include <iostream>

#include <chrono>

#include <thread>

using namespace std;

int main() {

  for (int x = 1; x <= 10; x++) {

    cout <<"x = "<< x << "\n";

    //Using sleep_for for 0.5 second delay

//    std::this_thread::sleep_for(500ms);

  }

  return 0;

}

Output:

CPP-Sleep_for_without

So, this time it took only 0.008s to execute the program.

Using usleep() function example

  • You may also use C++ usleep() function to delay the execution of code in C++ programs.
  • The usleep() function takes the duration in microseconds.
  • 1 second = 1000000 micro seconds
  • You may use this function for cross-platform compatibility, unlike the Sleep() function.
  • You have to include <unistd.h> in the header as it is required for usleep() function.

The example of using usleep() function

Notice this line of code in the program below where we used a for loop again:

usleep(1000000);

It will delay the execution of each iteration by one second i.e. 1000000 microseconds.

#include <iostream>

#include <unistd.h>     //required for usleep()

using namespace std;

int main() {

  //A for loop to run five times

  for (int x = 1; x <= 5; x++) {

    //Display current value of x

    cout << x << "\n";

    //Using usleep function to delay the execution by 1 second in each iteration

    usleep(1000000);

  }

  return 0;

}

Output:

CPP-usleep

You can see in the output above, the execution time is 5.008s (varies with the compiler).

This is because, in each iteration, usleep() function delayed the code execution by one second or 1000000 microseconds.

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!