Skip to content

Instantly share code, notes, and snippets.

@hgfernan
Created July 8, 2021 15:02
Show Gist options
  • Save hgfernan/cf6d395eeebf20712031de8e67295024 to your computer and use it in GitHub Desktop.
Save hgfernan/cf6d395eeebf20712031de8e67295024 to your computer and use it in GitHub Desktop.
// From https://pt.stackoverflow.com/questions/516989/c%c3%b3digo-compila-mas-n%c3%a3o-funciona-apenas-finaliza-com-sucesso
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *calculaPrimo(void *limitValue) {
int primo, j;
int num = 1;
int maxValue = limitValue;
while (num <= maxValue) {
primo = 1;
for (j = 2; j < num && primo; j++) {
if (num % j == 0) {
primo = 0;
}
}
if (primo == 1) {
printf("%d ", num);
}
num++;
}
printf("\n");
}
void criaThreads(char *args[]) {
int maxValue = atoi(args[1]);
int maxThreads = atoi(args[2]);
printf("%d , %d", maxValue, maxThreads);
long t;
pthread_t threads[maxThreads];
for (t = 0; t < maxThreads; t++) {
pthread_create(&threads[t], NULL,(void * ) calculaPrimo, &maxValue);
}
for (t = 0; t < maxThreads; t++) {
pthread_join(threads[t],NULL);
}
}
int main(int argc, int argv[]) {
char *args[] = {argv[0], argv[1], argv[2]};
criaThreads(args);
pthread_exit(NULL);
return 0;
}
@hgfernan
Copy link
Author

hgfernan commented Jul 8, 2021

Original file, from https://pt.stackoverflow.com page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment