Download presentation
Presentation is loading. Please wait.
1
CS 3340 Windows Programming
2
Test 3: Threading Barbershop Similar to Prog5 (Reader & Writer)
Tuesday, April 4 Lab 206 80 minutes Initial solution
3
One Barber with one Customer Queue
Barber Shop Customer Queue Barber One Barber with one Customer Queue
4
The queue could be empty
Before Opening Barber The queue could be empty
5
Barber goes to sleep if the Barber shop opens with an empty queue
6
There may be customers waiting in the queue
Before Opening C1 C2 C3 Barber There may be customers waiting in the queue
7
Barber Shop Opens Barber removes customer from the queue Barber
Barber wakes up customer after the hair cut
8
Barber Shop is Open C2 C3 C3 Barber C2 C1
9
Barber Shop is Open C3 Barber C3 C2
10
Barber goes to sleep when the queue is empty
Barber Shop is Open Barber Barber goes to sleep when the queue is empty
11
New Customer Comes when the Barber is Sleeping
Customer enters queue, wakes up barber, Then goes to sleep. Barber
12
New Customer Comes when the Barber is Sleeping
Barber will remove customer from the queue. Barber C4
13
New Customer Comes C5 C6 Barber C4
14
Barber should finish all waiting customers
Barber Shop Closing C5 C6 Barber should finish all waiting customers Barber C4
15
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
16
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
17
New customers don’t wait when shop is closing
Barber Shop Closing C9 New customers don’t wait when shop is closing Barber C6
18
Barber shop can open again
Barber Shop Closed Barber shop can open again Barber
19
New customers wait when shop is closed
Barber Shop Closed C10 C11 New customers wait when shop is closed Barber
20
Barber Shop Opens Barber removes customer from the queue Barber
Barber wakes up customer after the hair cut
21
Closing Before Exiting
Barber C11
22
Closing Before Exiting
Asking user Yes/No Barber
23
Customers come and wait
User No C1 C2 C3 Customers come and wait Barber
24
Exiting Before Opening
C1 C2 C3 Clear customer queue Barber
25
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
26
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
27
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.