Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@hgfernan
Created July 8, 2021 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hgfernan/c8564a48106f7db743c7321472fde382 to your computer and use it in GitHub Desktop.
Save hgfernan/c8564a48106f7db743c7321472fde382 to your computer and use it in GitHub Desktop.
*** tThread_orig.c 2021-07-08 11:31:40.000795883 -0300
--- tThread.c 2021-07-08 11:49:00.032924787 -0300
***************
*** 1,4 ****
! // 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>
--- 1,4 ----
! /* 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>
***************
*** 7,13 ****
void *calculaPrimo(void *limitValue) {
int primo, j;
int num = 1;
! int maxValue = limitValue;
while (num <= maxValue) {
primo = 1;
--- 7,13 ----
void *calculaPrimo(void *limitValue) {
int primo, j;
int num = 1;
! int maxValue = *(int*)limitValue;
while (num <= maxValue) {
primo = 1;
***************
*** 23,51 ****
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;
}
--- 23,63 ----
num++;
}
printf("\n");
+
+ return NULL;
}
! void criaThreads(char* const* args) {
! int maxValue;
! int maxThreads;
long t;
+ pthread_t *threads;
+
+ maxValue = atoi(args[1]);
+ maxThreads = atoi(args[2]);
+
+ printf("Maximum integer %d\n", maxValue);
+ printf("Number of threads %d\n", maxThreads);
! threads = (pthread_t *)calloc(sizeof(pthread_t), maxThreads);
!
for (t = 0; t < maxThreads; t++) {
! pthread_create(&threads[t], NULL, calculaPrimo, &maxValue);
}
+
for (t = 0; t < maxThreads; t++) {
pthread_join(threads[t],NULL);
}
}
! int main(int argc, char* const* argv) {
! if (argc < 3) {
! printf("%s: insufficient parameters. Please inform maximum value and number of threads\n", argv[0]);
! return 1;
! }
!
! criaThreads(argv);
pthread_exit(NULL);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment