0

I am trying to create a multi-threaded CPU benchmarking tool, and I'm unable to do the following: std::thread t[number_of_CPU_cores]; A compiler error is generated when trying to directly assign variable to thread t[] -

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

I therefore tried a stupid idea, which complicated things even more...

Can you help me with creating an array of threads dynamically? (I use the following to determine number of CPU cores)

int CoreCount = atoi(getenv(NUMBER_OF_PROCESSORS));


EDIT : Added an MCV example:

Compilation Errors:

  • C2672 'std::invoke': no matching overloaded function found <xthread> line 240
  • C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)' <xthread> line 240

Actual Code (very crude implementation - 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++;
    }
}

Platform Specifications: - OS: Windows 7 Ultimate SP1 - IDE: Visual Studio 2015 Community Update 2

  • 3
    What's the problem actually? Create a `std::vector threads(CoreCount);`? – πάντα ῥεῖ Jul 02 '16 at 03:07
  • I don't know STL - I'm yet to learn about it. –  Jul 02 '16 at 03:09
  • STL is more important than threads, learn it first. – jblixr Jul 02 '16 at 03:09
  • @AnandS I bet most of the questions asked on SO are by people who have "yet to learn" whatever the answer is. So now you know something new, it answers your question, now time to do some research on the suggestion / answer given. – PaulMcKenzie Jul 02 '16 at 03:10
  • 2
    @AnandS You can read more about `std::vector` (and other c++ standard library features) [here](http://en.cppreference.com/w/cpp/container/vector). Note that variable length arrays like `std::thread t[number_of_CPU_cores]` aren't c++ standard compliant. – πάντα ῥεῖ Jul 02 '16 at 03:16
  • @AnandS Also note that `int CoreCount = (int)getenv(NUMBER_OF_PROCESSORS);` doesn't do what you think it does. You probably need something like `int CoreCount = atoi(getenv(NUMBER_OF_PROCESSORS));` instread. – πάντα ῥεῖ Jul 02 '16 at 03:25
  • @PaulMcKenzie I shall certainly learn STL as much as possible, as I realize it's pretty simple, and its also very important. Thanks a ton. –  Jul 02 '16 at 04:28
  • @πάνταῥεῖ Thank you for your valuable inputs and references. I promise to learn STL as soon as possible. By the way, can you post an answer, building on the idea of `vector threads(CoreCount);`? –  Jul 02 '16 at 04:31
  • @AnandS Could you please provide a [MCVE] that reproduces your error? Otherwise it's very hard to anser your question conciously. – πάντα ῥεῖ Jul 02 '16 at 04:37
  • 2
    What about `std::thread::hardware_concurrency()`? It's portable unlike environment variables. – Blazo Jul 02 '16 at 04:37

0 Answers0