CS 3340 Windows Programming
Test 3: Threading Barbershop Similar to Prog5 (Reader & Writer) Tuesday, April 4 Lab 206 80 minutes Initial solution
One Barber with one Customer Queue Barber Shop Customer Queue Barber One Barber with one Customer Queue
The queue could be empty Before Opening Barber The queue could be empty
Barber goes to sleep if the Barber shop opens with an empty queue
There may be customers waiting in the queue Before Opening C1 C2 C3 Barber There may be customers waiting in the queue
Barber Shop Opens Barber removes customer from the queue Barber Barber wakes up customer after the hair cut
Barber Shop is Open C2 C3 C3 Barber C2 C1
Barber Shop is Open C3 Barber C3 C2
Barber goes to sleep when the queue is empty Barber Shop is Open Barber Barber goes to sleep when the queue is empty
New Customer Comes when the Barber is Sleeping Customer enters queue, wakes up barber, Then goes to sleep. Barber
New Customer Comes when the Barber is Sleeping Barber will remove customer from the queue. Barber C4
New Customer Comes C5 C6 Barber C4
Barber should finish all waiting customers Barber Shop Closing C5 C6 Barber should finish all waiting customers Barber C4
New customers don’t wait when shop is closing Barber Shop Closing C7 C5 C6 New customers don’t wait when shop is closing Barber C4
New customers don’t wait when shop is closing Barber Shop Closing C8 C6 New customers don’t wait when shop is closing Barber C5
New customers don’t wait when shop is closing Barber Shop Closing C9 New customers don’t wait when shop is closing Barber C6
Barber shop can open again Barber Shop Closed Barber shop can open again Barber
New customers wait when shop is closed Barber Shop Closed C10 C11 New customers wait when shop is closed Barber
Barber Shop Opens Barber removes customer from the queue Barber Barber wakes up customer after the hair cut
Closing Before Exiting Barber C11
Closing Before Exiting Asking user Yes/No Barber
Customers come and wait User No C1 C2 C3 Customers come and wait Barber
Exiting Before Opening C1 C2 C3 Clear customer queue Barber
Barber Thread Creating a new thread when the Barber shop opens The thread will be terminated when the Shop closed When the Shop opens again, a new thread is created The run sub of the Barber class is for one iteration of Open-Close to make code simpler
Prog5 ‘ Waits for all readers and writers to finish the work in order to ‘ terminate the program. ‘ Mutual exclusion on the DataObj and the queue must be enforced. Public Shared Sub FinishReadWrite() _database.LockDataObj() Monitor.Enter(FIFOQueue) If FIFOQueue.Count > 0 or _database.TheDatabaseStatus <> DataBaseClass.DatabaseStatus.Empty Then endProgram.Reset() Else endProgram.Set() End If Monitor.Exit(FIFOQueue) _database.ReleaseDataObj() endProgram.WaitOne() ‘do not know which thread will wake it up End Sub
JOIN Method Public Sub CloseBarberShop() Monitor.Enter(BarberShopClass.TheBarberStateObj) BarberShopClass.theBarberState = BarberShopClass.BarberState.Closing Monitor.Exit(BarberShopClass.TheBarberStateObj) barberEvent.Set() ‘ Wait until _barberThread is terminated _barberThread.Join() End Sub