根据CPU核心数动态分配线程数

时间:2016-07-02 03:02:19

标签: c++ multithreading

我正在尝试创建一个多线程CPU基准测试工具,我无法执行以下操作:std::thread t[number_of_CPU_cores];尝试直接将变量分配给thread t[]时会生成编译器错误 - < / p>

Only constant values can be assigned to std::thread t

因此,我尝试了一个愚蠢的想法,这使得事情更加复杂......

你能帮助我动态创建一个线程数组吗? (我使用以下内容确定CPU核心数)

int CoreCount = atoi(getenv(NUMBER_OF_PROCESSORS));

<小时/> 编辑:添加了一个MCV示例:

编译错误:

  • C2672'std :: invoke':找不到匹配的重载函数<xthread>第240行
  • C2893无法专门化功能模板'unknown-type std :: invoke(_Callable&amp;&amp;,_ Types&amp;&amp; ...)'<xthread>第240行

实际代码(非常粗略的实施 - SORRY !!!):

#include<iostream>
#include<cstdlib>
#include<conio.h>
#include<ctime>
#include<thread>

time_t start, endTime;
double IterCount = 0;

void RunBench();

int main()
{
    int CoreCount = atoi(getenv("NUMBER_OF_PROCESSORS"));

    cout<<"Press any key to begin!";
    getch();
    system("cls");
    cout<<"Running benchmark...";

    // Start, End time determined.
    start = time(NULL);
    endTime = start + 120;

    // Initialize n threads(n = Number_of_Processors); Beginning of benchmark.
    switch (CoreCount)
    {
        case 1:
        {
            thread t(RunBench);
            t.join();
            break;
        }
        case 2:
        {
            thread t[2];
            for (int i = 0; i < CoreCount; i++)
                t[i] = thread(RunBench, i); 
            for (int i = 0; i < CoreCount; ++i)
            {
                t[i].join();
            }
            break;
        }
        case 3:
        {
            thread t[3];
            for (int i = 0; i < CoreCount; i++)
                t[i] = thread(RunBench, i);
            for (int i = 0; i < CoreCount; ++i)
            {
                t[i].join();
            }
            break;
        }
        case 4:
        {
            thread t[4];
            for (int i = 0; i < CoreCount; i++)
                t[i] = thread(RunBench, i);
            for (int i = 0; i < CoreCount; ++i)
            {
                t[i].join();
            }
            break;
        }
        case 5:
        {
            thread t[5];
            for (int i = 0; i < CoreCount; i++)
                t[i] = thread(RunBench, i);
            for (int i = 0; i < CoreCount; ++i)
            {
                t[i].join();
            }
            break;
        }
        case 6:
        {
            thread t[6];
            for (int i = 0; i < CoreCount; i++)
                t[i] = thread(RunBench, i);
            for (int i = 0; i < CoreCount; ++i)
            {
                t[i].join();
            }
            break;
        }
        case 7:
        {
            thread t[7];
            for (int i = 0; i < CoreCount; i++)
                t[i] = thread(RunBench, i);
            for (int i = 0; i < CoreCount; ++i)
            {
                t[i].join();
            }
            break;
        }
        case 8:
        {
            thread t[8];
            for (int i = 0; i < CoreCount; i++)
                t[i] = thread(RunBench, i);
            for (int i = 0; i < CoreCount; ++i)
            {
                t[i].join();
            }
            break;
        }
        case 9:
        {
            thread t[9];
            for (int i = 0; i < CoreCount; i++)
                t[i] = thread(RunBench, i);
            for (int i = 0; i < CoreCount; ++i)
            {
                t[i].join();
            }
            break;
        }
        case 10:
        {
            thread t[10];
            for (int i = 0; i < CoreCount; i++)
                t[i] = thread(RunBench, i);
            for (int i = 0; i < CoreCount; ++i)
            {
                t[i].join();
            }
            break;
        }
        case 11:
        {
            thread t[11];
            for (int i = 0; i < CoreCount; i++)
                t[i] = thread(RunBench, i);
            for (int i = 0; i < CoreCount; ++i)
            {
                t[i].join();
            }
            break;
        }
        case 12:
        {
            thread t[12];
            for (int i = 0; i < CoreCount; i++)
                t[i] = thread(RunBench, i);
            for (int i = 0; i < CoreCount; ++i)
            {
                t[i].join();
            }
            break;
        }
    }

    cout<<"Score: "<<IterCount;

    return 0;
}

void RunBench()
{
    int a;
    double ans, i = 0;
    long long fact = 1;

    while (time(NULL) == endTime)
    {
        ans = sqrt(i);
        for (a = 1; a <= i; a++)
            fact = fact*a;
        i++;
        IterCount++;
    }
}

平台规格:   - 操作系统:Windows 7旗舰版SP1   - IDE:Visual Studio 2015社区更新2

0 个答案:

没有答案