Namespaces
Variants

std::jthread:: hardware_concurrency

From cppreference.net

Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
static unsigned int hardware_concurrency ( ) noexcept ;
(自 C++20 起)

返回实现所支持的并发线程数。该值应仅视为提示。

目录

参数

(无)

返回值

支持的并发线程数量。若该值未明确定义或无法计算,则返回 0

示例

#include <iostream>
#include <thread>
int main()
{
    unsigned int n = std::jthread::hardware_concurrency();
    std::cout << n << " concurrent threads are supported.\n";
}

可能的输出:

4 concurrent threads are supported.

参见

避免伪共享的最小偏移量
促进真共享的最大偏移量
(常量)