Skip to content

Instantly share code, notes, and snippets.

@eltonvs
Created October 19, 2016 21:46
Show Gist options
  • Save eltonvs/80034ac51a1ba60403d05a5b1393cf80 to your computer and use it in GitHub Desktop.
Save eltonvs/80034ac51a1ba60403d05a5b1393cf80 to your computer and use it in GitHub Desktop.
#include <thread>
#include <cstdio>
#include <chrono>
#include <vector>
#include <sched.h>
#include <unistd.h>
using namespace std;
#define NUMTHREADS 10
#define SECONDSRUNNING 120
int numExec[4][4];
void run(int i) {
for (int j = 0; j < SECONDSRUNNING * 2; j++) {
printf("Thread #%d está executando no núcleo %d\n", i, sched_getcpu());
numExec[i][sched_getcpu()]++;
this_thread::sleep_for(chrono::milliseconds(500));
}
}
int main(int argc, char *argv[]) {
vector<thread> threads;
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
numExec[i][j] = 0;
unsigned int n = thread::hardware_concurrency();
printf("PID = %i\n", getpid());
printf("Minha CPU possui %d núcleos\n", n);
for (int i = 0; i < NUMTHREADS; i++)
threads.push_back(thread(run,i));
for (auto &th : threads)
th.join();
int tot[4] = {0, 0, 0, 0};
for (int i = 0; i < 4; i++) {
printf("Thread %i:\n", i);
for (int j = 0; j < 4; j++) {
printf(" Core %i: %i\n", j, numExec[i][j]);
tot[j] += numExec[i][j];
}
}
printf("\nResumo Geral:\n");
for (int i = 0; i < 4; i++)
printf("Core %i: %i\n", i, tot[i]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment