Advertisement
milanmetal

[Linux-C] Mutex Example / pthread

Jan 26th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 KB | None | 0 0
  1. // https://stackoverflow.com/questions/13741243/passing-data-between-thread-using-c-issue
  2. // https://stackoverflow.com/questions/13741243/passing-data-between-thread-using-c-issue
  3. // https://stackoverflow.com/questions/13741243/passing-data-between-thread-using-c-issue
  4. // https://stackoverflow.com/questions/13741243/passing-data-between-thread-using-c-issue
  5.  
  6. #include <pthread.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9.  
  10. /*  
  11. The following structure contains the necessary information  
  12. to allow the function "dotprod" to access its input data and
  13. place its output into the structure.  
  14. */
  15.  
  16. typedef struct
  17.  {
  18.    double      *a;
  19.    double      *b;
  20.    double     sum;
  21.    int     veclen;
  22.  } DOTDATA;
  23.  
  24. /* Define globally accessible variables and a mutex */
  25.  
  26. #define NUMTHRDS 4
  27. #define VECLEN 100
  28.  
  29. DOTDATA dotstr; //GLOBAL DATA which is going to be accessed by different threads
  30.  
  31. pthread_t callThd[NUMTHRDS];
  32. pthread_mutex_t mutexsum;
  33.  
  34. void *dotprod(void *arg)
  35. {
  36.  
  37.    /* Define and use local variables for convenience */
  38.  
  39.    int i, start, end, len ;
  40.    long offset;
  41.    double mysum, *x, *y;
  42.    offset = (long)arg;
  43.  
  44.    len = dotstr.veclen;
  45.    start = offset*len;
  46.    end   = start + len;
  47.    x = dotstr.a;
  48.    y = dotstr.b;
  49.  
  50.    /*
  51.    Perform the dot product and assign result
  52.    to the appropriate variable in the structure.
  53.    */
  54.  
  55.    mysum = 0;
  56.    for (i=start; i<end ; i++)
  57.     {
  58.       mysum += (x[i] * y[i]);
  59.     }
  60.  
  61.    /*
  62.    Lock a mutex prior to updating the value in the shared
  63.    structure, and unlock it upon updating.
  64.    */
  65.    pthread_mutex_lock (&mutexsum);
  66.    dotstr.sum += mysum;
  67.    pthread_mutex_unlock (&mutexsum);
  68.  
  69.    pthread_exit((void*) 0);
  70. }
  71.  
  72. int main (int argc, char *argv[])
  73. {
  74.    long i;
  75.    double *a, *b;
  76.    void *status;    
  77.  
  78.    /* Assign storage and initialize values */
  79.    a = (double*) malloc (NUMTHRDS*VECLEN*sizeof(double));
  80.    b = (double*) malloc (NUMTHRDS*VECLEN*sizeof(double));
  81.  
  82.    for (i=0; i<VECLEN*NUMTHRDS; i++)
  83.     {
  84.      a[i]=1.0;
  85.      b[i]=a[i];
  86.     }
  87.  
  88.    dotstr.veclen = VECLEN;
  89.    dotstr.a = a;
  90.    dotstr.b = b;
  91.    dotstr.sum=0;
  92.  
  93.    pthread_mutex_init(&mutexsum, NULL);            
  94.  
  95.     for(i=0; i<NUMTHRDS; i++)
  96.         {
  97.     /*
  98.     Each thread works on a different set of data.
  99.     The offset is specified by 'i'. The size of
  100.     the data for each thread is indicated by VECLEN.
  101.     */
  102.     pthread_create(&callThd[i], NULL, dotprod, (void *)i);
  103.     }    
  104.  
  105.     /* Wait on the other threads */
  106. for(i=0; i<NUMTHRDS; i++)
  107.     {
  108.   pthread_join(callThd[i], &status);
  109. }    
  110.  
  111.    printf ("Sum =  %f \n", dotstr.sum);
  112.    free (a);
  113.    free (b);
  114.    pthread_mutex_destroy(&mutexsum);
  115.    pthread_exit(NULL);//No need of pthread_join() if pthread_exit() used.
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement