SHARE
TWEET

Untitled

LazySenpai Nov 24th, 2019 94 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <pthread.h>
  2. #include <math.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #define N 10
  7.  
  8. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  9. double sum=0;
  10. typedef struct {
  11. int x;
  12. int i;
  13. }argument;
  14.  
  15. int silnia(int n) {
  16.     if (n == 0) return 1;
  17.     if (n == 1) return 1;
  18.     return n*silnia(n-1);
  19. }
  20.  
  21. void* obliczIloczyn(void* ptr)
  22. {
  23.     argument* arg = ptr;
  24.     int dol = silnia(arg->i);    
  25.     double gora = pow((double)arg->x,(double)arg->i);
  26.     //printf("gora: %f\n",gora);
  27.     //printf("dol: %d\n",dol);
  28.     double dziel = (double)gora / (double)dol;
  29.     double* suma = malloc(sizeof(double));
  30.     *suma = dziel;
  31.    
  32.     pthread_mutex_lock(&mutex);
  33.    
  34.     sum+=*suma;
  35.    
  36.     pthread_mutex_unlock(&mutex);
  37.    
  38.     printf("suma: %f\n",*suma);
  39.     //mutex[arg->i] = (pthread_mutex_t)*suma;
  40. }
  41.  
  42. int main()
  43. {
  44.     double wynik=0;
  45.     double* result[N];
  46.     int i;
  47.     pthread_t watki[N];
  48.     for(i=0;i<N;i++)
  49.     {
  50.         argument* arg= malloc(sizeof(argument));
  51.         arg->x = 1;
  52.         arg->i = i;
  53.         pthread_create(&watki[i], NULL, obliczIloczyn, arg);
  54.     }
  55.     for(i=0;i<N;i++)
  56.     {
  57.         pthread_detach(watki[i]);
  58.     }
  59.    
  60.     printf("Wynik: %f\n",sum);
  61.     return 0;
  62. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top