CS%203340%20Windows%20Programming - PowerPoint PPT Presentation

About This Presentation
Title:

CS%203340%20Windows%20Programming

Description:

CS 3340 Windows Programming Test 3: Threading Barbershop Similar to Prog5 (Reader & Writer) Thursday, March 17 Lab 206 50 points Initial solutions * Barber Shop ... – PowerPoint PPT presentation

Number of Views:160
Avg rating:3.0/5.0
Slides: 30
Provided by: qya6
Category:

less

Transcript and Presenter's Notes

Title: CS%203340%20Windows%20Programming


1
CS 3340 Windows Programming
2
Test 3 Threading
  • Thursday, April 11
  • 1230 230
  • Before and after
  • Lab 206
  • Barbershop
  • Similar to Prog5 (Reader Writer)
  • Initial solution

3
Barber Shop
Customer Queue

Barber
One Barber with one Customer Queue
4
Before Opening

Barber
The queue could be empty
5
Barber Shop Opens

Barber
Barber goes to sleep if the Barber shop opens
with an empty queue
6
Before Opening
C1 C2 C3
Barber
There may be customers waiting in the queue
7
Barber Shop Opens
C1 C2 C3
C2 C3
Barber removes customer from the queue
Barber
C1
Barber wakes up customer after the hair cut
8
Barber Shop is Open
C2 C3
C3
Barber
C1
C2
9
Barber Shop is Open
C3

Barber
C2
C3
10
Barber Shop is Open

Barber
Barber goes to sleep when the queue is empty
11
New Customer Comes when the Barber is Sleeping
C4
C4
Customer enters queue, wakes up barber, Then goes
to sleep.
Barber
12
Barber is Working
C4

Barber will remove customer from the queue.
Barber
C4
13
New Customer Comes When the Barber is Working
C5 C6
Barber
C4
14
Barber Shop Closing
C5 C6
Barber should finish all waiting customers
Barber
C4
15
Barber Shop Closing
C7
C5 C6
New customers dont wait when shop is closing
Barber
C4
16
Barber Shop Closing
C8
C6
New customers dont wait when shop is closing
Barber
C5
17
Barber Shop Closing
C9

New customers dont wait when shop is closing
Barber
C6
18
Barber Shop Closed

Barber shop can open again
Barber
19
Barber Shop Closed
C10 C11
New customers wait when shop is closed
Barber
20
Barber Shop Opens
C10 C11
C11
Barber removes customer from the queue
Barber
C10
Barber wakes up customer after the hair cut
21
Closing Before Exiting
C11

Barber
C11
22
Closing Before Exiting

Asking user Yes/No
Barber
23
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
Barber State
  • Public Enum BarberState
  • Open
  • Working
  • Sleeping
  • Closing
  • Closed
  • End Enum

27
Barber 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

28
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 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

29
JOIN 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
Write a Comment
User Comments (0)
About PowerShow.com