I have the following test program with a simple function that finds primes which I am trying to run in multiple threads (just as an example).
#include <cstdio>
#include <iostream>
#include <ctime>
#include <thread>
void primefinder(void)
{
int n = 300000;
int i, j;
int lastprime = 0;
for(i = 2; i <= n; i++) {
for(j = 2; j <= i; j++) {
if((i % j) == 0) {
if(i == j)
lastprime = i;
else {
break;
}
}
}
}
std::cout << "Prime: " << lastprime << std::endl;
}
int main(void)
{
std::clock_t start;
start = std::clock();
std::thread t1(primefinder);
t1.join();
std::cout << "Time: " << (std::clock() - start) / (double)(CLOCKS_PER_SEC / 1000) << " ms" << std::endl;
start = std::clock();
std::thread t2(primefinder);
std::thread t3(primefinder);
t2.join();
t3.join();
std::cout << "Time: " << (std::clock() - start) / (double)(CLOCKS_PER_SEC / 1000) << " ms" << std::endl;
return 0;
}
As shown, I run the function once in 1 thread and then once in 2 different threads. I compile it with g++ using -O3 and -pthread. I am running it on Linux Mint 18. I have a Core i5-4670. I know it comes down to the OS but I would very much expect these threads to run in somewhat parallel. When I run the program, top shows 100% CPU when using 1 thread and 200% CPU when using 2 threads. Despite this the second run takes almost exactly twice as long.
The CPU is doing nothing else while running the program. Why doesn't this get executed in parallel ?
Aucun commentaire:
Enregistrer un commentaire