Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: August 12, 2025
Execution of any program requires the cooperation of the operating system. Usually, we don’t pay attention to this aspect of operation when a program places its variables in memory or performs CPU instructions. Sometimes, however, we need to involve the operating system more directly. One example is putting a program or thread to sleep for some time. When this happens, the operating system moves the program to the background, and when the sleep time expires, it brings it back.
In this tutorial, we’ll learn about sleep functions available in C and C++.
To compile code examples, we’ll need both C and C++ compilers. Thus, let’s ensure we have a popular GNU compilers collection installed. Let’s check the C compiler gcc version first:
$ gcc --version
gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
# ...
Then the C++ compiler g++:
$ g++ --version
g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
# ...
This version of g++ supports the C++17 standard, which we’ll use throughout this tutorial. If we don’t have these compilers, we need to install them. On Ubuntu, let’s use apt-get:
$ sudo apt-get install gcc
$ sudo apt-get install g++
In C++ we can use the sleep_for function defined in the thread header file. It’s a part of the C++ standard library. Let’s check it with a simple program sleep_for.cpp:
#include <thread> // std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds
#include <iostream> // std::cout, std::endl
int main()
{
std::cout << "About to sleep ..." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5));
std::cout << "Woken up!" << std::endl;
}
Note using headers to provide object declaration, e.g. #include <thread>. Without it, we would obtain compilation errors, like “error: ‘std::this_thread’ has not been declared”. Now we can compile this code and create an executable file sleep_for:
$ g++ sleep_for.cpp -o sleep_for
Then, let’s start it:
$ ./sleep_for
About to sleep ...
Woken up!
Note that we don’t provide sleeping time as a number in a default unit. Instead, we use an object representing a duration in seconds, std::chrono::seconds(5). Thus, we can express our intention more exactly. Finally, let’s list all predefined objects suitable to express time periods:
std::chrono::hours
std::chrono::minutes
std::chrono::seconds
std::chrono::milliseconds
std::chrono::microseconds
std::chrono::nanoseconds
We can make our code more concise with the help of C++ literals. They were invented so that we can naturally use time units, e.g., 36h. Let’s rewrite our previous example:
#include <thread> // std::this_thread::sleep_for
#include <chrono> // std::chrono::seconds, std::chrono_literals
#include <iostream> // std::cout, std::endl
int main()
{
using namespace std::chrono_literals; // namespace of literals
using namespace std::this_thread; // namespace of sleep_for
std::cout << "About to sleep ..." << std::endl;
sleep_for(5s);
std::cout << "Woken up!" << std::endl;
}
Here, a literal 5s provides a five-second long duration. We should not separate 5 from s by a space, like ‘5 s’.
We can find predefined literals for all durations mentioned previously. So, let’s create variables that take each of these units in the time_literals.cpp program:
#include <chrono> // std::chrono::duration, std::chrono_literals
#include <iostream> // std::cout, std::endl
int main()
{
using namespace std::chrono_literals; // namespace of literals
std::chrono::duration nanoperiod{300ns};
std::chrono::duration microperiod{45us};
std::chrono::duration milliperiod{900ms};
std::chrono::duration secondsperiod{5.3s};
std::chrono::duration minutesperiod{10min};
std::chrono::duration hoursperiod{1.5h};
std::cout << "nanoperiod lasts for " << nanoperiod.count() << "ns" << std::endl
<< "microperiod lasts for " << microperiod.count() << "um" << std::endl
<< "milliperiod lasts for " << milliperiod.count() << "ms" << std::endl
<< "secondsperiod lasts for " << secondsperiod.count() << "s" << std::endl
<< "minutesperiod lasts for " << minutesperiod.count() << "min" << std::endl
<< "hoursperiod lasts for " << hoursperiod.count() << "h" << std::endl;
}
We use the count function of the duration variables, which returns the number of ticks within the period. Let’s compile the code:
$ g++ time_literals.cpp -o time_literals
Afterward, let’s check the result:
$ ./time_literals
nanoperiod lasts for 300ns
microperiod lasts for 45um
milliperiod lasts for 900ms
secondsperiod lasts for 5.3s
minutesperiod lasts for 10min
hoursperiod lasts for 1.5h
The standard library offers a second way to create a delay. The sleep_until function causes the current thread to sleep until some moment in the future. So we need to express an absolute time in our program. To do this, we need a time_point object. Here we have a simple example:
#include <thread> // std::this_thread::sleep_until
#include <chrono> // std::chrono::duration, std::chrono::time_point, std::chrono_literals
#include <iostream> // std::cout, std::endl
int main()
{
using namespace std::chrono_literals; //namespace for literals
std::cout << "About to sleep ..." << std::endl;
std::chrono::time_point start{std::chrono::steady_clock::now()};
std::this_thread::sleep_until(start + 15s);
std::cout << "Woken up!" << std::endl;
}
Let’s highlight important points:
In C, we can use the function nanosleep, which provides a nanosecond granularity. Let’s study a simple example in the nano_sleep.c file:
#include <stdio.h> /* for printf */
#include <time.h> /* for timespec and nanosleep */
int main()
{
printf("About to sleep ... \n");
struct timespec td = {
.tv_sec = 5, /* 5 seconds */
.tv_nsec = 200*1000*1000 /* and 200ms */
};
nanosleep(&td, NULL);
printf("Woken up!\n");
}
We need to compile this code with gcc:
$ gcc nano_sleep.c -o nano_sleep
Finally, we can run it:
$ ./nano_sleep
About to sleep ...
Woken up!
The nanosleep function takes a pointer to the td structure of type timespec. Therein, we can set two intervals of time, tv_sec for seconds and tv_nsec for nanoseconds. The function sums them up. Additionally, the function takes the second argument. Here we pass NULL, but it can point to another timespec structure. It would contain a remaining time if the sleep were interrupted.
We’ll use the thrd_sleep function from the C threads library as another solution. The library comes with the C11 C standard and is designed to support concurrent thread execution. Let’s check it:
#include <threads.h> /* for thrd_sleep */
#include <stdio.h> /* for printf */
#include <time.h> /* for timespec */
int main(void)
{
printf("About to sleep ... \n");
struct timespec td = {
.tv_sec = 5 /*5 seconds*/
};
thrd_sleep(&td, NULL);
printf("Woken up!\n");
}
As with nanosleep, we must prepare timespec structure td to pass the desired sleep period.
Yet another way to pause program execution is to use the sleep command or program of the operating system. In most Linux distributions, we have the sleep command at our disposal by default. To call it from a C program, we use the system function from the stdlib.h header:
#include <stdlib.h> /* for system */
#include <stdio.h> /* for printf */
int main(void)
{
printf("About to sleep ... \n");
system("sleep 10");
printf("Woken up!\n");
}
As the program waits for the system function to complete, it will be delayed by 10 seconds, following the specification of the Linux sleep command.
Creating code that compiles on different platforms without modifications is always a good idea, even if we have solutions that make cross-compilation easier. Regarding portability, we can use all the described C++ functions equally well on a Windows machine. They come with the C++ standard library implemented by Microsoft Visual C++. In the C realm, the thrd_sleep is also accessible in both Linux and Windows.
Unfortunately, Windows API doesn’t provide nanosleep, a POSIX-compliant function. Its closest equivalent is the Sleep function. However, attempting to use Sleep with the gcc compiler will leave us with the “‘Sleep’ was not declared in this scope” error. In other words, nanosleep is Unix, Sleep is Windows.
The system function is a different story. This function is available in both gcc and Microsoft Visual C++ platforms. But, Windows OS doesn’t provide a sleep command by default, so we need to alter the system‘s argument accordingly.
In this tutorial, we looked at ways to delay program execution in C and C++ programming languages. First, we used C++ functions sleep_for and sleep_until. On this occasion, we studied ways of expressing time durations and time points in this language. Next, we moved to C and learned about the nanosleep and thrd_sleep functions.
Note that all mentioned sleep variations provide nanosecond time resolution. However, any delay may be slightly longer than specified due to overhead associated with the system waking up the thread. Thus, the duration of five seconds means sleep for no less than that. Similarly, sleeping until a particular hour means waking up no earlier than this hour.
Finally, we introduced the system function, which allowed the execution of the system command. We applied it to start the Linux sleep.