Advertisement
Guest User

c++

a guest
Jun 14th, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.55 KB | None | 0 0
  1. // Filosofi.cpp : Defines the entry point for the console application.
  2. //
  3. // Date: 12.06.2013
  4. // Time: 22:06
  5. // Semaphores example program
  6.  
  7. #include "stdafx.h"
  8. #include "windows.h"
  9. #include "iostream"
  10. #include "process.h"
  11. #include "math.h"
  12.  
  13. using namespace std;
  14.  
  15. //global variables
  16. //hMutex used for blocking threads
  17. HANDLE hMutex;
  18. //used to block entrance to access to check if mutex is blocked
  19. int gCurrentAvailableSemaphores = 3;
  20. //obvious
  21. int gRequiredIterations=0;
  22. //program output - factorial of iteration count value
  23. long gOutput = 1;
  24.  
  25. //global enum used to set colors of text and backgroind in console using names of colors
  26. enum ConsoleColor
  27. {
  28.     Black         = 0,
  29.     Blue          = 1,
  30.     Green         = 2,
  31.     Cyan          = 3,
  32.     Red           = 4,
  33.     Magenta       = 5,
  34.     Brown         = 6,
  35.     LightGray     = 7,
  36.     DarkGray      = 8,
  37.     LightBlue     = 9,
  38.     LightGreen    = 10,
  39.     LightCyan     = 11,
  40.     LightRed      = 12,
  41.     LightMagenta  = 13,
  42.     Yellow        = 14,
  43.     White         = 15
  44. };
  45.  
  46. //sets the console output text color to extText  and background to extBackground
  47. void SetColor(ConsoleColor extText, ConsoleColor extBackground)
  48. {  
  49.     HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  50.     SetConsoleTextAttribute(hStdOut, (WORD)((extBackground << 4) | extText));
  51. }
  52.  
  53.  
  54. class semaphore {
  55.     bool state;
  56.     public:
  57.         //constructor
  58.         semaphore() {
  59.             state = true;
  60.         }
  61.         void setState(bool ex_state) {
  62.             state = ex_state;
  63.         }
  64.         bool getState() const{
  65.             return state;
  66.         }
  67. };
  68.  
  69. class philosophist {
  70.     int forkA, forkB, number;
  71. public:
  72.     philosophist(int ex_number, int ex_forkAval, int ex_forkBval) {
  73.         forkA = ex_forkAval;
  74.         forkB = ex_forkBval;
  75.         number = ex_number;
  76.     }
  77.     int getForkA () {return forkA;}
  78.     int getForkB () {return forkB;}
  79.     int getID() {return number;}
  80. };
  81.  
  82. //all semaphores in one array
  83. semaphore *gSems=new semaphore[5];
  84.  
  85. //static thread function __stdcall required for _beginthreadex
  86. static unsigned __stdcall philosophistFunction(void *params)
  87. {
  88.     //getting id from thread params
  89.     philosophist *ph = static_cast<philosophist*>(params);
  90.     do {
  91.         //if forks are available
  92.         if (gSems[ph->getForkA()].getState() && gSems[ph->getForkB()].getState()) {
  93.             //reducing available semaphores count
  94.             gSems[ph->getForkA()].setState(false);
  95.             gSems[ph->getForkB()].setState(false);
  96.             Sleep(100);
  97.             SetColor(Black,White);         
  98.             cout << "Philosophist #"<<ph->getID()<<" gets his spoons and started eating"<<endl;
  99.             Sleep(1800);
  100.             cout << "Philosophist #"<<ph->getID()<<" finished eating"<<endl;
  101.             Sleep(100);
  102.             gSems[ph->getForkA()].setState(true);
  103.             gSems[ph->getForkB()].setState(true);
  104.         }
  105.         else {
  106.             Sleep(250);
  107.             cout << "Philosophist #"<<ph->getID()<<" is waiting for his spoons become available"<<endl;
  108.             Sleep(250);
  109.         }          
  110.     } while (true);
  111.     return NULL;   
  112. }
  113.  
  114. int _tmain(int argc, _TCHAR* argv[])
  115. {
  116.     //creating threads
  117.     philosophist *ph1 = new philosophist(1,0,1);
  118.     _beginthreadex(NULL, 0, &philosophistFunction, &ph1, 0, 0);
  119.     Sleep(50);
  120.     philosophist *ph2 = new philosophist(2,1,2);
  121.     _beginthreadex(NULL, 0, &philosophistFunction, &ph2, 0, 0);
  122.     Sleep(50);
  123.     philosophist *ph3 = new philosophist(3,2,3);
  124.     _beginthreadex(NULL, 0, &philosophistFunction, &ph3, 0, 0);
  125.     Sleep(50);
  126.     philosophist *ph4 = new philosophist(4,3,4);
  127.     _beginthreadex(NULL, 0, &philosophistFunction, &ph4, 0, 0);
  128.     Sleep(50);
  129.     philosophist *ph5 = new philosophist(5,4,5);
  130.     _beginthreadex(NULL, 0, &philosophistFunction, &ph5, 0, 0);
  131.     //loopin till the end
  132.     cin;
  133.     return 0;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement