Guest User

Untitled

a guest
May 30th, 2015
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. #define M (4*500000000)
  6. #define N 4
  7.  
  8. static volatile int count = 0;
  9. static int *res;
  10.  
  11. static int do_calc (int x)
  12. {
  13.     int res;
  14.  
  15.     res = x ^ (x >> 16);
  16.     res *= 0xcafebabe;
  17.     res = res ^ (x >> 8);
  18.     res *= 0xfeedabba;
  19.     return res;
  20. }
  21.  
  22. void* start (void *arg)
  23. {
  24.     int start, len, i;
  25.  
  26.     start = __sync_add_and_fetch (&count, 1);
  27.     len = M / N;
  28.     start = (start-1)*len;
  29.    
  30.     for (i = start; i < start+len; i++) res[i] = do_calc(i);
  31.     return NULL;
  32. }
  33.  
  34. int main ()
  35. {
  36.     int i;
  37.     pthread_t threads[N];
  38.     res = malloc (sizeof(int)*M);
  39.  
  40.     //start(NULL);
  41.     for (i = 0; i < N; i++)
  42.         pthread_create (&(threads[i]), NULL, start, NULL);
  43.     for (i = 0; i < N; i++)
  44.         pthread_join (threads[i], NULL);
  45.  
  46.     printf ("%i\n", count);
  47.     /* for (i=0; i<M; i++) */
  48.     /*     if (do_calc(i) != res[i]) printf ("%i %i\n", do_calc(i), res[i]); */
  49.     free (res);
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment