Lab Discussion Notes for March 1st, 1999


 

Generally speaking...
You should do something such as this for the barber problem...
 

1) add private data to the class BarberShop

2) start the barber and the customers in ThreadTest()

3) BarberShop Constructor

4) BarberShop Destructor

5) void BarberShop::serveNextCustomer()

6) void BarberShop::enter(int i)


 

Take Note:

There are many ways to implement this problem and they can all be correct.  So, depending on how you choose to represent certain objects, your solutions may vary.  If you are solving this problem in another way, that is fine.  This is just one general suggestion of how to start thinking about it.

It is by no means, an entirely complete pseudocode.  There are signals and waits and other such things that I have definitely not explicitly mentioned here.  This is because knowing where (in your code) to put those is an integral part of understanding this material.


 

1) add private data to the class BarberShop

   you may need...

 number of chairs (not including the barber's) in the shop
 but you may want to use a semaphore for this, it depends

 number of customers waiting
 lock or mutex
 some condition variables


2) start the barber and the customers in ThreadTest()
 

1 barber
n customers (from standard input)

do this the same way you did for the producers and consumers


3) BarberShop Constructor

   BarberShop::BarberShop(int n)
 

passed in the number of chairs, n
initialize number of chairs

set the number of customers waiting to initial value
create lock, mutexes and conditions that you have
        added to part (1) above

4) BarberShop Destructor

   BarberShop::~BarberShop()

   delete anything you called new to create in the constructor
 
 

5) void BarberShop::serveNextCustomer()

   get the lock or mutex
 
   if no one is waiting, go to sleep
          print out relevant information about barber sleeping and such

          barber has to wait for someone to signal him (wake him up)

   ...otherwise if somone is waiting, or someone wakes him up....
         barber cuts their hair

   release the lock or mutex
   yield the current thread
 
 

6) void BarberShop::enter(int i)

   get the lock or mutex

   if all the chairs are full

 print customer leaving without haircut line
 release the lock or mutex
 leave the function


   otherwise if the barber is sleeping

print that you're waking him up
wake him up
sit in a chair and get your haircut


   otherwise

 print that you are waiting
 sit in a chair and wait (however you plan to implement this action)
 increment the number of customers waiting variable
 
   decrement the number of customers waiting
   release the lock or mutex