Advertisement
KsaneK

C - multithreading - calculating sinus of given args

Nov 10th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <math.h>
  5.  
  6. struct pairs
  7. {
  8.     double val;
  9.     double res;
  10. };
  11.  
  12. void * calcThread(void * val)
  13. {
  14.         struct pairs * pair = (struct pairs *)val;
  15.         pair->res = sin(pair->val*M_PI/180);
  16.         pthread_exit(0);
  17. }
  18.  
  19. int main(int argc, char * argv[])
  20. {
  21.         int i = 1;
  22.         struct pairs * values = (struct pairs *)malloc((argc-1) * sizeof(struct pairs));
  23.         pthread_t * threads = (pthread_t *)malloc((argc-1) * sizeof(pthread_t));
  24.         void * tmp;
  25.         for(i; i < argc; i++)
  26.         {
  27.                 values[i-1].val = strtod(argv[i], NULL);
  28.                 pthread_create(&(threads[i-1]), NULL, &calcThread, (void *)&(values[i-1]));
  29.         }
  30.         for(i = 0; i < argc - 1; i++)
  31.         {
  32.                 pthread_join(threads[i], tmp);
  33.                 printf("THREAD %lu: sin(%lf) = %lf\n", pthread_self(), values[i].val, values[i].res);
  34.         }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement