Created
July 8, 2021 15:02
-
-
Save hgfernan/cf6d395eeebf20712031de8e67295024 to your computer and use it in GitHub Desktop.
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
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Original file, from https://pt.stackoverflow.com page.