Title: CS%203340%20Windows%20Programming
1CS 3340 Windows Programming
2Test 3 Threading
- Thursday, April 11
- 1230 230
- Before and after
- Lab 206
- Barbershop
- Similar to Prog5 (Reader Writer)
- Initial solution
3Barber Shop
Customer Queue
Barber
One Barber with one Customer Queue
4Before Opening
Barber
The queue could be empty
5Barber Shop Opens
Barber
Barber goes to sleep if the Barber shop opens
with an empty queue
6Before Opening
C1 C2 C3
Barber
There may be customers waiting in the queue
7Barber Shop Opens
C1 C2 C3
C2 C3
Barber removes customer from the queue
Barber
C1
Barber wakes up customer after the hair cut
8Barber Shop is Open
C2 C3
C3
Barber
C1
C2
9Barber Shop is Open
C3
Barber
C2
C3
10Barber Shop is Open
Barber
Barber goes to sleep when the queue is empty
11New Customer Comes when the Barber is Sleeping
C4
C4
Customer enters queue, wakes up barber, Then goes
to sleep.
Barber
12Barber is Working
C4
Barber will remove customer from the queue.
Barber
C4
13New Customer Comes When the Barber is Working
C5 C6
Barber
C4
14Barber Shop Closing
C5 C6
Barber should finish all waiting customers
Barber
C4
15Barber Shop Closing
C7
C5 C6
New customers dont wait when shop is closing
Barber
C4
16Barber Shop Closing
C8
C6
New customers dont wait when shop is closing
Barber
C5
17Barber Shop Closing
C9
New customers dont wait when shop is closing
Barber
C6
18Barber Shop Closed
Barber shop can open again
Barber
19Barber Shop Closed
C10 C11
New customers wait when shop is closed
Barber
20Barber Shop Opens
C10 C11
C11
Barber removes customer from the queue
Barber
C10
Barber wakes up customer after the hair cut
21Closing Before Exiting
C11
Barber
C11
22Closing Before Exiting
Asking user Yes/No
Barber
23User No
C1 C2 C3
Customers come and wait
Barber
24Exiting Before Opening
C1 C2 C3
Clear customer queue
Barber
25Barber 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
26Barber State
- Public Enum BarberState
- Open
- Working
- Sleeping
- Closing
- Closed
- End Enum
27Barber Run Method
- While Not done
- Lock this object to access the barber state
- If the State is Open
- . . .
- Else If the State is Sleeping
- . . .
- Else If the State is Closing
- . . .
- Else If the State is Closed
- . . .
- Else (Working)
- . . .
- End While
28Prog5
- 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 gt 0 or _database.TheDatabase
Status ltgt - DataBaseClass.DatabaseSt
atus.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
29JOIN Method in Barber Class
- The dummy thread executes the code
- Public Sub CloseBarberShop()
- Monitor.Enter(Me)
- _theState BarberState.Closing
- Monitor.Exit(Me)
- barberEvent.Set()
- The dummy thread waits here until
_barberThread is terminated - _barberThread.Join()
- End Sub