Thread hardware_concurrency() function in C++

Last Updated : 8 Jul, 2026

std::thread::hardware_concurrency() is a static member function of the std::thread class that returns the number of threads the underlying hardware can execute concurrently. It is defined in the <thread> header and is commonly used to determine the optimal number of worker threads for parallel programs.

  • Returns the number of hardware threads supported by the system.
  • Helps choose an appropriate number of threads for multithreaded applications.
C++
#include <iostream>
#include <thread>
using namespace std;

int main()
{
    cout << thread::hardware_concurrency();

    return 0;
}

Output
4

Explanation: The function returns the number of concurrent hardware threads available on the system. The actual value depends on the processor and operating system.

Syntax

std::thread::hardware_concurrency();

  • Parameters: This function does not accept any parameters.
  • Return Value: It returns a non-negative integer denoting the

Working of hardware_concurrency()

The function queries the underlying hardware and operating system to estimate the number of threads that can run simultaneously.

  • Returns the number of logical CPU cores available.
  • Does not create or manage threads.
  • Provides only a recommendation for thread count.
  • May return 0 if the information is unavailable.

Example: Using hardware_concurrency()

CPP
#include <iostream>
#include <thread>
using namespace std;

int main()
{
    unsigned int threads = thread::hardware_concurrency();

    cout << "Supported concurrent threads: "
         << threads << endl;

    return 0;
}

Output
Supported concurrent threads: 4

Common Uses

The hardware_concurrency() function is commonly used to:

  • Determine the number of worker threads to create.
  • Optimize parallel algorithms.
  • Balance workload across CPU cores.
  • Improve resource utilization in multithreaded programs.

Advantages

Using hardware_concurrency() provides several benefits:

  • Helps optimize thread creation and Improves CPU utilization.
  • Reduces unnecessary thread overhead.
  • Makes multithreaded applications more scalable.

Limitations

Despite its usefulness, hardware_concurrency() has some limitations:

  • The returned value is implementation-dependent.
  • It may return 0 if the hardware information is unavailable.
  • Performance also depends on workload and system resources.

Note: When compiling programs that use the <thread> library with g++, you may need to enable POSIX thread support: g++ -std=c++14 -pthread file.cpp

Comment