Last active
August 29, 2015 14:11
-
-
Save tjukic/77d8ff8d11b026eb0491 to your computer and use it in GitHub Desktop.
Prime calculator, multicore (auto max)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CAUTION: This code puts your CPU at 100% load. | |
#include <string> | |
#include <iostream> | |
#include <thread> | |
#include <math.h> | |
void task() | |
{ | |
int N = 999999999; | |
for (int i = 2; N > 0; ++i) | |
{ | |
bool isPrime = true; | |
for (int j = 2; j < i; ++j) { | |
if (i % j == 0) { isPrime = false; break; }} | |
if (isPrime) { --N; } | |
} | |
} | |
int main() { | |
int concurentThreadsSupported = std::thread::hardware_concurrency(); | |
std::cout << concurentThreadsSupported; | |
std::thread* t = new std::thread[concurentThreadsSupported](); | |
for (int i = 0; i < concurentThreadsSupported; ++i) { t[i] = std::thread(task); } | |
for (int i = 0; i < concurentThreadsSupported; ++i) { t[i].join(); } | |
std::cin.get(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment