如何确定最大线程数?

How to determine the maximum number of threads?

我正在学习并行编程。我将 OpenMP 与 C++ 一起使用,我想了解如何确定我可以在笔记本电脑上设置的最大并行线程数。在文档中我找到了函数 omp_get_max_threads(),但是如何确保 return 值正确?

当我编译这段代码时:

#include <omp.h>
#include <iostream>
using namespace std;
int main()
{
   cout << omp_get_max_threads() << "\n";
    
#pragma omp parallel 
        {

           
        }
        return 0;

}

输出为:8

当这个:

#include <omp.h>
#include <iostream>
using namespace std;
int main()
{

    
#pragma omp parallel 
        {
            cout << omp_get_max_threads();
           
        }
        return 0;

}

输出为:88888888

当您想了解可以使用多少个线程时。您可以通过调用 std::thread::hardware_concurrency().

来完成此操作