Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. typedef struct
  2. {
  3. pthread_t tid;
  4. int inicio;
  5. int fim;
  6. long double pi;
  7. } pi_struct;
  8.  
  9. void* calcula_pi(void *args);
  10.  
  11. int main(int argc, int * argv[])
  12. {
  13. //pi_struct *aux = (pi_struct *)args;
  14. struct pi_struct aux[10];
  15. int aux_inicio = 0;
  16. int aux_fim = 100;
  17. int counter = 2;
  18. int i;
  19.  
  20. for(i = 0; i < 10; i++)
  21. {
  22. aux[i].inicio = aux_inicio;
  23. aux[i].fim = aux_fim;
  24.  
  25. pthread_create(&aux[i], NULL, calcula_pi, (void*)&aux[i]);
  26. aux_inicio = aux_fim;
  27. aux_fim = aux_fim * counter;
  28. counter++;
  29. }
  30.  
  31. printf("\nPi = %.20Lf\n", aux->pi);
  32.  
  33. return 0;
  34. }
  35.  
  36. void* calcula_pi(void *args)
  37. {
  38. double temp;
  39. pi_struct *aux = (pi_struct *)args;
  40.  
  41. for(; aux->inicio < aux->fim; aux->inicio++)
  42. {
  43. temp = (pow((int)-1, (int)aux->inicio));
  44. aux->pi += (temp / ((2 * aux->inicio) + 1));
  45. }
  46.  
  47. pthread_exit(NULL);
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement