thunderboltsid

flipping

Sep 21st, 2016
387
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>
  5. #include <time.h>
  6. #include <stdint.h>
  7.  
  8. #define _NUMBER_OF_FLIPS 1
  9. #define _NUMBER_OF_COINS 20
  10. #define _NUMBER_OF_PEOPLE 10000
  11.  
  12. char coins[_NUMBER_OF_COINS];
  13.  
  14. pthread_mutex_t lock;
  15. pthread_mutex_t locks[_NUMBER_OF_COINS];
  16.  
  17. struct person {
  18.     pthread_t tid;
  19. };
  20. struct person people[_NUMBER_OF_PEOPLE];
  21.  
  22. void *flip_coins_first(void *arg) {
  23.     pthread_mutex_lock(&lock);
  24.     for (int i = 0; i < _NUMBER_OF_COINS; i++) {
  25.         for (int j = 0; j < _NUMBER_OF_FLIPS; j++) {
  26.             if (coins[i] == 'X') {
  27.                 coins[i] = '0';
  28.             } else {
  29.                 coins[i] = 'X';
  30.             }
  31.         }
  32.     }
  33.     pthread_mutex_unlock(&lock);
  34.     return NULL;
  35. }
  36.  
  37. void *flip_coins_second(void *arg) {
  38.     int idx = (intptr_t) arg;
  39.     pthread_mutex_lock(&locks[idx]);
  40.     for (int j = 0; j < _NUMBER_OF_FLIPS; j++) {
  41.         if (coins[idx] == 'X') {
  42.             coins[idx] = '0';
  43.         } else {
  44.             coins[idx] = 'X';
  45.         }
  46.     }
  47.     pthread_mutex_unlock(&locks[idx]);
  48.     return NULL;
  49. }
  50.  
  51. int main(int argc, char *argv[]) {
  52.     pthread_mutex_init(&lock, NULL);
  53.     for (int i = 0; i < _NUMBER_OF_COINS; i++) {
  54.         pthread_mutex_init(&locks[i], NULL);
  55.     }
  56.     pthread_t orig_tid = pthread_self();
  57.     for (int i = 0; i < _NUMBER_OF_COINS; i++) {
  58.         coins[i] = '0';
  59.     }
  60.     clock_t begin = clock();
  61.     printf("coins: %s (start - global lock)\n", coins);
  62.     for (int i = 0; i < _NUMBER_OF_PEOPLE; i++) {
  63.         int err = pthread_create(&people[i].tid, NULL, flip_coins_first,
  64.                                  NULL);
  65.         if (err != 0) {
  66.             printf("Can't spawn thread %d", i);
  67.         }
  68.     }
  69.     for (int i = 0; i < _NUMBER_OF_PEOPLE; i++) {
  70.         (void) pthread_join(people[i].tid, NULL);
  71.     }
  72.     printf("coins: %s (end - global lock)\n", coins);
  73.     pthread_mutex_destroy(&lock);
  74.     clock_t end = clock();
  75.     double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  76.     printf("%d threads x %d flips: %f ms\n\n", _NUMBER_OF_PEOPLE,
  77.            _NUMBER_OF_FLIPS, time_spent);
  78.     begin = clock();
  79.     printf("coins: %s (start - local lock)\n", coins);
  80.     for (int i = 0; i < _NUMBER_OF_PEOPLE; i++) {
  81.         for (int j = 0; j < _NUMBER_OF_COINS; j++) {
  82.             int err = pthread_create(&people[i].tid, NULL, flip_coins_second,
  83.                                      (void *) (intptr_t) j);
  84.             if (err != 0) {
  85.                 printf("Can't spawn thread %d", i);
  86.             }
  87.         }
  88.     }
  89.     for (int i = 0; i < _NUMBER_OF_PEOPLE; i++) {
  90.         (void) pthread_join(people[i].tid, NULL);
  91.     }
  92.     printf("coins: %s (end - local lock)\n", coins);
  93.     end = clock();
  94.     time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  95.     printf("%d threads x %d flips: %f ms\n", _NUMBER_OF_PEOPLE,
  96.            _NUMBER_OF_FLIPS, time_spent);
  97.     for (int i = 0; i < _NUMBER_OF_COINS; i++) {
  98.         pthread_mutex_destroy(&locks[i]);
  99.     }
  100.  
  101.     return 0;
  102. }
RAW Paste Data