Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include "windows.h"
- #include <iostream>
- #include <thread>
- #define ROUNDS 5
- int CAPACITY;
- int BEES_NUMBER;
- using namespace std;
- int pot;
- bool BeeWokePooh = false;
- bool BeeWokePiglet = false;
- //Mutex(мьютекс) - mutual exception(объект взаимного исключения)
- DWORD iVal = 0;
- HANDLE hMutex = NULL;
- DWORD WINAPI Pooh(LPVOID lpParam) {
- CONST HANDLE hMutex = (CONST HANDLE)lpParam;
- while (true) {
- WaitForSingleObject(hMutex, INFINITE);
- cout << this_thread::get_id() << "\t***WINNIE THE POOH IS SLEEPING***\n";
- ReleaseMutex(hMutex);
- Sleep(200);
- if (BeeWokePooh == true && pot >= 4) {
- pot -= 4;
- cout << this_thread::get_id() << "\t***WINNIE THE POOH EATS HONEY FROM POT***\n \tPOT HAVE " << pot << " PARTS OF HOHEY\n";
- BeeWokePooh = false;
- }
- }
- ExitThread(0);
- }
- DWORD WINAPI Piglet(LPVOID lpParam) {
- CONST HANDLE hMutex = (CONST HANDLE)lpParam;
- while (true) {
- WaitForSingleObject(hMutex, INFINITE);
- cout << this_thread::get_id() << "\t***PIGLET IS SLEEPING***\n";
- ReleaseMutex(hMutex);
- Sleep(100);
- if (BeeWokePiglet == true && pot >=1) {
- pot -= 1;
- cout << this_thread::get_id() << "\t***PIGLET EATS HONEY FROM POT***\n \tPOT HAVE " << pot << " PARTS OF HOHEY\n";
- BeeWokePiglet = false;
- }
- }
- ExitThread(0);
- }
- DWORD WINAPI Bee(CONST LPVOID lpParam) {
- CONST HANDLE hMutex = (CONST HANDLE)lpParam;
- cout << this_thread::get_id() << "\t===BEE STARTS WORKING===\n";
- for(DWORD i = 0; i < ROUNDS; i++){
- WaitForSingleObject(hMutex, INFINITE);
- ReleaseMutex(hMutex);
- if (pot == CAPACITY) {
- cout << "\tTHE POT IS FULL\n";
- if (pot >= 4)
- BeeWokePooh = true;
- if (pot >= 1)
- BeeWokePiglet = true;
- break;
- } else {
- pot++;
- cout << this_thread::get_id() << "\t+++POT+++\n";}
- if (pot >= 4)
- BeeWokePooh = true;
- if (pot >= 1)
- BeeWokePiglet = true;
- }
- cout << this_thread::get_id() << "\t===BEE GOES HOME===\n" << "\tPOT HAVE " << pot << " PARTS OF HOHEY\n" ;
- ExitThread(0);
- }
- int enter_n(string msg) {
- int n;
- while (true)
- {
- cout << msg << endl;
- cin >> n;
- if (cin.fail())
- {
- cin.clear();
- cin.ignore(32767, '\n');
- cout << "Oops, that input is invalid. Please try again.\n";
- }
- else
- {
- cin.ignore(32767, '\n');
- return n;
- }
- }
- }
- INT main() {
- pot = 0;
- BEES_NUMBER = enter_n("Enter a value of bee population");
- CAPACITY = enter_n("Enter a value of capacity of pot");
- HANDLE *hThreads = new HANDLE[BEES_NUMBER];
- hMutex = CreateMutex(NULL, FALSE, NULL);
- if (NULL == hMutex) {
- cout << "Failed to create mutex\n";
- }
- HANDLE hPooh = CreateThread(NULL, 0, &Pooh, hMutex, 0, NULL);
- Sleep(10);
- HANDLE hPiglet = CreateThread(NULL, 0, &Piglet, hMutex, 0, NULL);
- Sleep(10);
- for (DWORD i = 0; i < BEES_NUMBER; i++) {
- hThreads[i] = CreateThread(NULL, 0, &Bee, hMutex, 0, NULL);
- Sleep(50);
- if (NULL == hThreads[i]) {
- cout << "Failed to created thread\n";
- }
- }
- WaitForMultipleObjects(BEES_NUMBER, hThreads, TRUE, INFINITE);
- for (DWORD i = 0; i < BEES_NUMBER; i++) {
- CloseHandle(hThreads[i]);
- }
- delete[] hThreads;
- CloseHandle(hMutex);
- ExitProcess(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement