Skip to content

std::thread (C++11) ​

In a Nutshell ​

The C++ Standard Library's native thread wrapper: constructing an object starts the underlying OS thread right away, giving you true multi-task concurrency.

#include <thread>

Core API Cheat Sheet ​

OperationSignatureDescription
Constructorthread() noexcept;Default construction; not associated with any thread
Constructortemplate< class Function, class... Args > explicit thread( Function&& f, Args&&... args );Constructs and immediately starts a thread
Destructor~thread();Must already be joined or detached before destruction, otherwise std::terminate is called
Assignmentthread& operator=( thread&& other ) noexcept;Move assignment
Joinablebool joinable() const noexcept;Checks whether the thread can be joined (i.e., whether it is associated with an active thread)
Joinvoid join();Blocks the current thread and waits for the target thread to finish
Detachvoid detach();Separates the thread from the thread object so it runs independently in the background
Get IDid get_id() const noexcept;Returns the thread identifier
Hardware Concurrencystatic unsigned int hardware_concurrency() noexcept;Returns the number of concurrent threads supported by the implementation

Minimal Example ​

C++
#include <iostream>
#include <thread>

void task(int n) {
    for (int i = 0; i < n; ++i)
        std::cout << "worker: " << i << "\n";
}

int main() {
    std::thread t(task, 3);
    t.join(); // Block until thread t finishes executing
    std::cout << "done\n";
}
// Standard: C++11

Embedded Applicability: High ​

  • Zero abstraction overhead: std::thread maps directly onto the underlying OS thread (an RTOS task or a POSIX pthread, for example)
  • hardware_concurrency() can probe the available core count at runtime to size a thread pool dynamically
  • Combined with std::mutex and std::atomic, it can safely guard shared peripheral registers or global buffers
  • Watch out for OS thread stack overhead (typically a few KB to a few tens of KB); on very small-memory MCUs we need to tightly control thread count and stack size

Compiler Support ​

GCCClangMSVC
4.63.119.0

See Also ​


Some content is adapted from cppreference.com, used under the CC-BY-SA 4.0 license

pdf-latest-4-g85128cc · 85128cc · 2026-10-05