Advertisement
Leeen

Winnie The Pooh and Piglet

May 12th, 2020
4,490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "pch.h"
  2. #include "windows.h"
  3. #include <iostream>
  4. #include <thread>
  5. #define ROUNDS 5
  6.  
  7. int CAPACITY;
  8. int BEES_NUMBER;
  9.  
  10.  
  11. using namespace std;
  12.  
  13. int pot;
  14. bool BeeWokePooh = false;
  15. bool BeeWokePiglet = false;
  16.  
  17. //Mutex(мьютекс) - mutual exception(объект взаимного исключения)
  18. DWORD iVal = 0;
  19. HANDLE hMutex = NULL;
  20.  
  21. DWORD WINAPI Pooh(LPVOID lpParam) {
  22.     CONST HANDLE hMutex = (CONST HANDLE)lpParam;
  23.    
  24.     while (true) {
  25.         WaitForSingleObject(hMutex, INFINITE);
  26.         cout << this_thread::get_id() << "\t***WINNIE THE POOH IS SLEEPING***\n";
  27.         ReleaseMutex(hMutex);
  28.         Sleep(200);
  29.         if (BeeWokePooh == true && pot >= 4) {
  30.             pot -= 4;
  31.             cout << this_thread::get_id() << "\t***WINNIE THE POOH EATS HONEY FROM POT***\n \tPOT HAVE " << pot << " PARTS OF HOHEY\n";
  32.             BeeWokePooh = false;
  33.         }
  34.     }
  35.     ExitThread(0);
  36. }
  37.  
  38. DWORD WINAPI Piglet(LPVOID lpParam) {
  39.     CONST HANDLE hMutex = (CONST HANDLE)lpParam;
  40.  
  41.     while (true) {
  42.         WaitForSingleObject(hMutex, INFINITE);
  43.         cout << this_thread::get_id() << "\t***PIGLET IS SLEEPING***\n";
  44.         ReleaseMutex(hMutex);
  45.         Sleep(100);
  46.  
  47.         if (BeeWokePiglet == true && pot >=1) {
  48.             pot -= 1;
  49.             cout << this_thread::get_id() << "\t***PIGLET EATS HONEY FROM POT***\n \tPOT HAVE " << pot << " PARTS OF HOHEY\n";
  50.             BeeWokePiglet = false;
  51.         }
  52.     }
  53.     ExitThread(0);
  54. }
  55.  
  56. DWORD WINAPI Bee(CONST LPVOID lpParam) {
  57.     CONST HANDLE hMutex = (CONST HANDLE)lpParam;
  58.    
  59.     cout << this_thread::get_id() << "\t===BEE STARTS WORKING===\n";
  60.     for(DWORD i = 0; i < ROUNDS; i++){
  61.     WaitForSingleObject(hMutex, INFINITE);
  62.    
  63.     ReleaseMutex(hMutex);
  64.  
  65.     if (pot == CAPACITY) {
  66.         cout << "\tTHE POT IS FULL\n";
  67.         if (pot >= 4)
  68.             BeeWokePooh = true;
  69.         if (pot >= 1)
  70.             BeeWokePiglet = true;
  71.      
  72.         break;
  73.     } else {  
  74.         pot++;
  75.         cout << this_thread::get_id() << "\t+++POT+++\n";}
  76.         if (pot >= 4)
  77.             BeeWokePooh = true;
  78.         if (pot >= 1)
  79.             BeeWokePiglet = true;
  80.     }
  81.  
  82.     cout << this_thread::get_id() << "\t===BEE GOES HOME===\n" << "\tPOT HAVE " << pot << " PARTS OF HOHEY\n" ;
  83.     ExitThread(0);
  84. }
  85. int enter_n(string msg) {
  86.     int n;
  87.     while (true)
  88.     {
  89.         cout << msg << endl;
  90.         cin >> n;
  91.         if (cin.fail())
  92.         {
  93.             cin.clear();
  94.             cin.ignore(32767, '\n');
  95.             cout << "Oops, that input is invalid.  Please try again.\n";
  96.         }
  97.         else
  98.         {
  99.             cin.ignore(32767, '\n');
  100.  
  101.             return n;
  102.         }
  103.     }
  104. }
  105.  
  106. INT main() {
  107.    
  108.     pot = 0;
  109.     BEES_NUMBER = enter_n("Enter a value of bee population");
  110.     CAPACITY = enter_n("Enter a value of capacity of pot");
  111.     HANDLE *hThreads = new HANDLE[BEES_NUMBER];
  112.     hMutex = CreateMutex(NULL, FALSE, NULL);
  113.     if (NULL == hMutex) {
  114.         cout <<  "Failed to create mutex\n";
  115.     }
  116.     HANDLE hPooh = CreateThread(NULL, 0, &Pooh, hMutex, 0, NULL);
  117.     Sleep(10);
  118.     HANDLE hPiglet = CreateThread(NULL, 0, &Piglet, hMutex, 0, NULL);
  119.     Sleep(10);
  120.     for (DWORD i = 0; i < BEES_NUMBER; i++) {
  121.         hThreads[i] = CreateThread(NULL, 0, &Bee, hMutex, 0, NULL);
  122.         Sleep(50);
  123.         if (NULL == hThreads[i]) {
  124.             cout << "Failed to created thread\n";
  125.         }
  126.     }
  127.     WaitForMultipleObjects(BEES_NUMBER, hThreads, TRUE, INFINITE);
  128.  
  129.     for (DWORD i = 0; i < BEES_NUMBER; i++) {
  130.         CloseHandle(hThreads[i]);
  131.      
  132.     }
  133.     delete[] hThreads;
  134.     CloseHandle(hMutex);
  135.     ExitProcess(0);
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement