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