Advertisement
Guest User

Untitled

a guest
Nov 16th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. /**
  2. * Written 16. November 2014 by Kai Nilsen
  3. */
  4. #include <stdio.h>
  5. #include <pthread.h>
  6. #include <string>
  7. #include <iostream>
  8. #include <sstream>
  9. using namespace std;
  10.  
  11. int const NUM_THREADS = 3;
  12.  
  13. pthread_t threads[NUM_THREADS];
  14.  
  15. int global = 0;
  16. int globalOut = 0;
  17.  
  18. bool quit = false;
  19.  
  20. pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
  21.  
  22. pthread_cond_t ready = PTHREAD_COND_INITIALIZER, work = PTHREAD_COND_INITIALIZER, print = PTHREAD_COND_INITIALIZER;
  23.  
  24. int input();
  25.  
  26. void* inFunc(void* param)
  27. {
  28.     int number;
  29.  
  30.     int loop = 0;
  31.  
  32.     while (quit != true)
  33.     {
  34.         pthread_mutex_lock(&lock);
  35.         if (global == 0 && globalOut == 0)
  36.         {
  37.  
  38.             number = input();
  39.  
  40.             if (number != 0)
  41.             {
  42.                 printf("  inFunc sending %d\n", number);
  43.  
  44.                 global = number;
  45.             }
  46.             else
  47.             {
  48.                 global = number;
  49.                 quit = true;
  50.             }
  51.  
  52.         }
  53.         pthread_cond_signal(&work);
  54.         pthread_mutex_unlock(&lock);
  55.        
  56.         loop++;
  57.         pthread_cond_wait(&ready, &lock);
  58.     }
  59.  
  60.     printf("inFunc looped %d\n", loop);
  61.     pthread_exit(0);
  62.     return 0;
  63. }
  64.  
  65. void* workFunc(void* param)
  66. {
  67.     int loop = 0;
  68.     while (quit != true)
  69.     {
  70.        
  71.         pthread_mutex_lock(&lock);
  72.         pthread_cond_wait(&work, &lock);
  73.         if (global != 0 && globalOut == 0)
  74.         {
  75.  
  76.             if (global != 0)
  77.             {
  78.                 globalOut = global * 3;
  79.                 printf("  workFunc turning %d into %d\n", global, globalOut);
  80.                 global = 0;
  81.             }
  82.  
  83.         }
  84.        
  85.         pthread_mutex_unlock(&lock);
  86.         pthread_cond_signal(&print);
  87.         loop++;
  88.     }
  89.     printf("workFunc looped %d\n", loop);
  90.     pthread_exit(0);
  91.     return 0;
  92. }
  93.  
  94. void* outFunc(void* param)
  95. {
  96.     int loop = 0;
  97.     while (quit != true)
  98.     {
  99.        
  100.         pthread_mutex_lock(&lock);
  101.         pthread_cond_wait(&print, &lock);
  102.        
  103.         if (globalOut != 0 && global == 0)
  104.         {
  105.  
  106.             if (globalOut != 0)
  107.             {
  108.                 printf("  outFunc printing %d\n", globalOut);
  109.                 globalOut = 0;
  110.             }
  111.  
  112.         }
  113.        
  114.         pthread_mutex_unlock(&lock);
  115.         pthread_cond_signal(&ready);
  116.         loop++;
  117.     }
  118.     printf("outFunc looped %d\n", loop);
  119.     pthread_exit(0);
  120.     return 0;
  121. }
  122.  
  123. int main()
  124. {
  125.  
  126.     pthread_create(&threads[0], NULL, inFunc, NULL);
  127.     pthread_create(&threads[1], NULL, workFunc, NULL);
  128.     pthread_create(&threads[2], NULL, outFunc, NULL);
  129.  
  130.     for (int i = 0; i < NUM_THREADS; i++)
  131.     {
  132.         pthread_join(threads[i], NULL);
  133.     }
  134.     pthread_mutex_destroy(&lock);
  135.     system("pause");
  136.     return 0;
  137. }
  138.  
  139. int input()
  140. {
  141.     char input[10];
  142.     int number;
  143.     bool validNumber = false;
  144.  
  145.  
  146.     while (validNumber != true)
  147.     {
  148.  
  149.         printf("> ");
  150.         gets_s(input);
  151.  
  152.         for (int i = 0; i < strlen(input); i++)
  153.         {
  154.             if (i == 0 && input[0] == '-' && (input[1] <= '9' && input[1] >= '0'))
  155.             {
  156.                 validNumber = true;
  157.             }
  158.             else if (input[i] <= '9' && input[i] >= '0')
  159.             {
  160.                 validNumber = true;
  161.             }
  162.             else
  163.             {
  164.                 validNumber = false;
  165.                 break;
  166.             }
  167.         }
  168.  
  169.         if (validNumber == false && input[0] == 'q' && strlen(input) == 1)
  170.         {
  171.             number = 0;
  172.             validNumber = true;
  173.         }
  174.         else if (validNumber == true)
  175.         {
  176.             number = stoi(input);
  177.         }
  178.     }
  179.  
  180.     return number;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement