C++ Threading

Table of Contents

1. std::this_thread namespace

this_thread provides functions for accessing the current executing thread.

  • std::this_thread::yield() hints the OS to reschedule the execution of threads, allowing other threads to run.
  • sleep_for(time) blocks the current thread for at least the specified time.
  • sleep_until(time_point) blocks the execution of the current thread until specified time_point has been reached. This time_point is recommended to use std::chrono::steady_clock.

2. std::jthread

Threads begin execution immediately upon construction of the associated thread object, starting from the function (top-level function) provided as constructor argument. The returned value of top-level function is ignored. If it terminates by throwing an exception, std::terminate() is called. The top-level function may also communicate its returned value or an exception to the caller via std::promise or by modifying shared variables.

join()
blocks the current thread until the thread identified by *this finishes its execution.

Date: 2026-06-13 Sat