Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Concurrency

std::thread::hardware_concurrency

Reference for std::thread::hardware_concurrency(): what the hint means, when it returns 0, and how to size a worker pool and split work safely.

std::thread::hardware_concurrency() is a static member function that returns a hint for the number of hardware threads the program can run in parallel: typically the number of logical cores, counting hyper-threads. It is the usual starting point when you decide how many worker threads to create.

The value is only a hint. The standard allows the function to return 0 when the number cannot be determined, and the result says nothing about how many cores your process may really use (containers, CPU quotas and affinity masks can restrict it). Because it varies from machine to machine, never let the result of your program depend on it; only the amount of parallelism should.

C++
unsigned n = std::thread::hardware_concurrency();
unsigned workers = n == 0 ? 2 : n;

Try it

C++ A safe worker count

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++ Splitting a sum across workers

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++
static unsigned int std::thread::hardware_concurrency() noexcept;
static unsigned int std::jthread::hardware_concurrency() noexcept;   // same function

The function takes no parameters.

Returns: the number of concurrent threads supported by the implementation, or 0 if the value is not well defined or not computable.

Errors / undefined behavior: none; it is noexcept.

Usage notes

  • Treat 0 as "unknown". Always handle it, for example with a default of 2 or 4, and enforce a minimum of one worker.
  • It is a hint, not a promise. The OS may give your process fewer cores than it reports (cgroups, Docker --cpus, job objects). On our runner there is one CPU and the sandbox may report more or fewer than that.
  • Cache it. On some platforms each call performs a system query. Read it once and reuse the value.
  • Not a limit. You can start more threads than cores. For I/O-bound work that is common; for CPU-bound work extra threads only add switching cost.
  • Choose the count to suit the work. For CPU-bound loops a good default is hardware_concurrency() workers. When the main thread also works, use one less.
  • Keep results independent of the count. Chunk the data, give each worker its own output slot and combine after join(); the sum, the sorted order, and so on should come out identical.
  • Hyper-threading counts. Two logical cores on one physical core do not give twice the throughput for arithmetic-heavy code.
  • Related constant: std::hardware_destructive_interference_size (in <new>, C++17) is the cache-line size for padding per-thread data to avoid false sharing. GCC warns when you use it in a header-like context; alignas(64) is the common portable-in-practice choice.