Extra Credit  - Use the Win32 API to solve the Barber problem, as outlined in class (see web overhead). Fork off 1 barber & 5customers & 1 monitor thread that continuously prints out the status of the shop. Make the waiting room size be 3. The customers run continuously until the barbers signals them that they have received their last haircut (then they die). The barber does his barber thing continuously until signaled by the monitor thread that it's time to go. The barber then signals the customers & waits for all to leave, then leaves himself. The monitor thread can then return to main. The monitor can be used to check & report barbershop status & allow you to signal the barber to quit.

 

#include <iostream.h>

 

#define CUTS      30

#define HAIRCUTS  10

#define MAXWAIT    5

 

// full of timing problems

class BarberShop

{  int chair, bell, waitCount, cutting; // flags

   void getCust();

   void cutHair();

   void waitTilCalled();

   void waitTilCut();

 public:

   BarberShop(){chair=waitCount=cutting=bell=0;}

   void barber();

   void customer();

};

 

void BarberShop::barber()

{ int headCount = 0; // # of customers today

 

  while(headCount < CUTS)

  { if (waitCount)    // customers waiting

    { getCust();     // bring customer in from lobby

      waitCount--;   // ***

      chair++;       // seat customer in chair

      cutHair();

      headCount++;

      chair--;       // customer leaves

    }

    else              // no customers, take a nap

    { while(!bell);  // sleep - busy wait

      bell = 0;      // turn off bell

    }

  }

}

 

void BarberShop::customer()

{ int haircuts = 0;  // # of haircuts received

 

  while(haircuts < HAIRCUTS)

  { if (waitCount < MAXWAIT)  // enter lobby

    {   if (waitCount == 0 && chair == 0) // nobody there

       {  bell = 1;            // wake barber

          waitCount ++;       // ***

          waitTilCalled();    // sit in lobby til called

          waitTilCut();       // sit in chair while haircut

          haircuts++;

       }

       // leave shop

    }

    // grow hair

  }

}

 

int main(int argc, char **argv)

{  Barbershop bs;

 

   // fork off 1 barber & N customers

        return 0;

}