Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Concurrency

The std::this_thread namespace

Reference for std::this_thread: get_id, sleep_for, sleep_until and yield, with examples of timing guarantees and why sleeping is a poor synchronization tool.

std::this_thread (header <thread>) is a namespace of four functions that act on the thread that calls them: get_id() returns its identifier, sleep_for() and sleep_until() pause it, and yield() offers the rest of its time slice to other threads. Because they always refer to the calling thread, they work the same in main and in any thread you start.

Sleeping guarantees a minimum delay only: the thread sleeps for at least the given time, and may wake up later because of scheduling. That is fine for a pause, a retry back-off or a demo, but it is never a correct way to make one thread wait for another. Use join, a future, a latch or a condition variable for that.

C++
using namespace std::chrono_literals;
std::this_thread::sleep_for(20ms);
std::thread::id me = std::this_thread::get_id();

Try it

C++ get_id, sleep_for and sleep_until

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ yield in a spin-wait, and why it is not a fix

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
std::thread::id std::this_thread::get_id() noexcept;
template<class Rep, class Period>
void std::this_thread::sleep_for(const std::chrono::duration<Rep, Period>& d);
template<class Clock, class Duration>
void std::this_thread::sleep_until(const std::chrono::time_point<Clock, Duration>& t);
void std::this_thread::yield() noexcept;
Function Description
get_id() Returns the std::thread::id of the calling thread. Ids can be compared, hashed and streamed (operator<<), but the printed text is unspecified.
sleep_for(d) Blocks for at least the duration d. A zero or negative duration returns at once.
sleep_until(t) Blocks until the clock reaches t. If t is already past, it returns at once.
yield() A hint to the scheduler to run another thread. Does not block and may do nothing.

Returns: get_id() returns an id; the others return nothing. Errors / undefined behavior: none under normal use; the sleep functions may throw whatever exceptions a custom clock or duration throws.

Usage notes

  • Prefer sleep_until in loops that must keep a steady rhythm: compute next += 100ms and sleep until it, so drift does not accumulate the way repeated sleep_for(100ms) does.
  • Use steady_clock. Wall-clock (system_clock) can jump when the system time is changed, which affects sleep_until on it.
  • Sleeping is not synchronization. "Sleep 100 ms and then assume the other thread has finished" is a race that works until the machine is busy. Wait on the actual event.
  • Granularity: the real delay is rounded up to the OS timer resolution, so tiny sleeps (microseconds) take longer than asked.
  • A sleeping thread cannot be woken early by sleep_for. If the sleep must be cancellable, wait on a condition_variable with a timeout, or on a std::stop_token callback, instead.
  • yield() is a polite spin. It reduces the cost of busy-waiting but the loop still burns CPU. For waits of unknown length, block on a condition variable or a semaphore.
  • Thread ids: std::thread::id{} (default constructed) means "not a thread". Printing ids is fine for logging, but their value is not portable and must not be parsed or compared against constants.
  • On the main thread these functions also work; sleep_for in main simply delays the program.
  • Compare with std::thread::get_id(), the member function of a std::thread object, which returns the id of that thread rather than the caller.