welcome: please sign in

Cerca

Link Dipartimentali

Link Esterni

Allegato "bancoCheckinSol.cpp"

Scarica

   1 #include <JTC/JTC.h>
   2 #include <iostream>
   3 #include <time.h>
   4 
   5 using namespace std;
   6 
   7 #define MAX 40
   8 
   9 class Posti : public JTCMonitor
  10 {
  11 	int posto[MAX];
  12 public:
  13 	int trovaEBloccaPosto(int codiceutente)
  14 	{
  15 		JTCSynchronized sync(*this);
  16 		for(int i =0; i < MAX; i++)
  17 			if (posto[i] == 0)
  18 			{
  19 				posto[i] = codiceutente;
  20 				return i;
  21 			}
  22 		return -1;
  23 	}
  24 };
  25 Posti* posti;
  26 
  27 class StampaSync : public JTCMonitor
  28 
  29 {
  30 public:
  31 	void stampa(int cod, int utente, int posto, bool esitoPos)
  32 	{
  33 		JTCSynchronized sync(*this);
  34         cout << "------" << endl;
  35 		cout << "Banco checkin: " << cod <<endl;
  36 		   if (esitoPos)
  37      	{
  38      		cout << "Prenotato posto: " << posto << " per utente " << utente << endl;
  39      	}
  40      	else
  41      		cout << "\tUtente: " << utente <<
  42      		" non c'è posto per te." << endl;
  43 	}
  44 };
  45 
  46 StampaSync *stampaSync;
  47 
  48 class BancoCheckin : public JTCThread
  49 
  50 {
  51 	int codiceBanco;
  52 public:
  53 	BancoCheckin (int cod)
  54 	{ codiceBanco = cod; }
  55 	virtual void run()
  56 	{
  57 		srand(codiceBanco*time(0));
  58 		while(true)
  59 		{
  60 		   sleep(100);
  61 		   int codiceutente = rand() % 10000;
  62 		   int posto = posti->trovaEBloccaPosto(codiceutente);
  63 		   stampaSync->stampa(codiceBanco,codiceutente,posto, posto > 0);
  64 		}
  65 	}
  66 };
  67 
  68 int main()
  69 {
  70 	JTCInitialize init;
  71 	posti = new Posti();
  72 	stampaSync = new StampaSync();
  73     BancoCheckin *a = new BancoCheckin(1);
  74     BancoCheckin *b = new BancoCheckin(2);
  75     BancoCheckin *c = new BancoCheckin(3);
  76     a->start();
  77     b->start();
  78     c->start();
  79     return 0;
  80 }

Allegati

Non è consentito inserire allegati su questa pagina.