Skip to content

Instantly share code, notes, and snippets.

@tjukic
Last active August 29, 2015 14:11
Show Gist options
  • Save tjukic/77d8ff8d11b026eb0491 to your computer and use it in GitHub Desktop.
Save tjukic/77d8ff8d11b026eb0491 to your computer and use it in GitHub Desktop.
Prime calculator, multicore (auto max)
// 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