Advertisement
Guest User

Untitled

a guest
Feb 28th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5.  
  6. long arr[10000];
  7.  
  8. void *find_first_half(void* arg) {
  9. double *sin_sum = malloc(sizeof(double));
  10. double temp;
  11.  
  12. int i;
  13. for(i = 0; i < 5000; i++) {
  14. temp += sin(arr[i]);
  15. }
  16.  
  17. sin_sum = &temp;
  18. return (void *) sin_sum;
  19. }
  20.  
  21. void *find_second_half(void* arg) {
  22. double *sin_sum = malloc(sizeof(double));
  23. double temp;
  24. int i;
  25. for(i = 5000; i < 10000; i++) {
  26. *sin_sum += sin((double)arr[i]);
  27. }
  28.  
  29. sin_sum = &temp;
  30. return (void *) sin_sum;
  31. }
  32.  
  33. int main() {
  34. int i;
  35. for(i = 0; i < 10000; i++) {
  36. arr[i] = i;
  37. }
  38.  
  39. pthread_t th[2];
  40.  
  41. pthread_create(&th[0], NULL, find_first_half, NULL);
  42. pthread_create(&th[1], NULL, find_second_half, NULL);
  43.  
  44. void *first_half, *second_half;
  45.  
  46. pthread_join(th[0], &first_half);
  47. pthread_join(th[1], &second_half);
  48.  
  49. printf("%f\n", *(double*)first_half + *(double *)second_half);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement