Post on 19-Jan-2016
1
Prog4
user_thread
. . .amount = …
invoke delegate transact (amount). . .
mainThread
. . .Total + = amount …
user_thread
. . .amount = …
invoke delegate transact (amount). . .
user_thread
. . .amount = …
invoke delegate transact (amount). . .
Total
2
Data Shared by Multiple Threads
user_thread
. . .amount = …
Total += amount. . .
user_thread
. . .amount = …
Total += amount. . .
user_thread
. . .amount = …
Total += amount. . .
Total
3
Time Sharing
threadOne
threadTwo
Time line
The threads don’t know when the time out will occur.
4
Data Shared by Multiple Threads
user_thread
. . .amount = …
Total += amount// In assembly// Load Total// Add amount// Save Total
. . .
Total
user_thread
. . .amount = …
Total += amount// In assembly// Load Total// Add amount// Save Total
. . .
5
Mutual Exclusion
user_thread
Start runningamount = 50
Total += 50// In assembly// Load TotalTotal: 1000Time Out
Continue runningTotal: 1000// Add amount// Save TotalTotal: 1050
. . .
Total: 1000 900 1050
user_thread
Start runningamount = -100
Total += -100// In assembly// Load TotalTotal: 1000// Add amount// Save TotalTotal: 900
. . .
Critical Section
When accessing shared data.
Should be executed as a single statement.
Total += amount
// Load Total
// Add amount
// Save Total6
Two Classes for Mutual Exclusion
• MutEx
• Monitor
7
8
Class MutEx (Semaphore)
A synchronization primitive that can also be used for inter-process synchronization.
When two or more threads need to access a shared resource at the same time, the system needs a synchronization mechanism to ensure that only one thread at a time uses the resource.
MutEx is a synchronization primitive that grants exclusive access to the shared resource to only one thread.
If a thread acquires a MutEx, the second thread that wants to acquire that MutEx is suspended until the first thread releases the MutEx.
9
Class MutEx Methods
• WaitOne:
Call it before accessing shared resource Blocks the current thread until receiving a signal
• ReleaseMutEx:
Call it after accessing shared resource Releases the MutEx once
. . .
10
Using MutEx Object
user_thread
Start runningamount = 50
MutExObj.WaitOne(CriticalSection)Total += 50// In assembly// Load TotalTotal: 1000Time Out
Continue runningTotal: 1000// Add amount// Save TotalTotal: 1050MutExObj.Release
. . .
MutExObj
Total: 1000 1050 950
user_thread
Start runningamount = -100
MutExObj.WaitOne(CriticalSection)Total += -100// In assembly// Load TotalTotal: 1050// Add amount// Save TotalTotal: 950MutExObj.Release
. . .
11
Class Monitor
Provides a mechanism that synchronizes access to objects.
The Monitor class controls access to objects by granting a lock for an object to a single thread.
Object locks provide the ability to restrict access to a block of code, commonly called a critical section.
While a thread owns the lock for an object, no other thread can acquire that lock.
12
Class Monitor Features
• It is associated with an object on demand.
• It is unbound, which means it can be called directly from any context.
• An instance of the Monitor class cannot be created.
13
Class Monitor
The following information is maintained for each synchronized object:
• A reference to the thread that currently holds the lock.
• A reference to a ready queue, which contains the threads that are ready to obtain the lock.
• A reference to a waiting queue, which contains the threads that are waiting for notification of a change in the state of the locked object.
14
Monitor Methods
• Enter • Exit
• TryEnter • Wait • Pulse • PulseAll• . . .
15
Using Class Monitor
Private Total As Integer‘ Cannot use Monitor on integersPrivate TotalObj As New Object
user_thread
. . .amount = 50
Monitor.Enter(TotalObj)(CriticalSection)Total += 50// In assembly// Load Total// Add amount// Save TotalMonitor.Exit(TotalObj). . .
16
Using Class Monitor
user_thread
Start runningamount = 50
Monitor.Enter(TotalObj)(CriticalSection)Total += 50// In assembly// Load TotalTotal: 1000Time Out
Continue runningTotal: 1000// Add amount// Save TotalTotal: 1050Monitor.Exit(TotalObj)
. . .
TotalObj
Total: 1000 1050 950
user_thread
Start runningamount = -100
Monitor.Enter(TotalObj)(CriticalSection)Total += -100// In assembly// Load TotalTotal: 1050// Add amount// Save TotalTotal: 950Monitor.Exit(TotalObj)
. . .
17
Using Monitor in Class MethodPublic Class ManagerClass Private Total As Integer Private TotalObj As New Object
Public Sub UpdateTotal(ByVal x As Integer)
Monitor.Enter(TotalObj)
‘ CriticalSection
Total += x
Monitor.Exit(TotalObj)
End Sub . . .End Class
Public Class ObjClass Private obj As ManagerClass = New ManagerClass Private amount As Integer
Private Sub Run . . . ‘ generate amount obj.UpdateTotal(amount) . . . End SubEnd Class
18
Using Monitor in Class Method
obj of objClass
. . .obj.UpdateTotal(amount). . . ManagerClass
Private Total As IntegerPrivate TotalObj As New Object
Public Sub UpdateTotal(. . .) Monitor.Enter(TotalObj) Total += x Monitor.Exit(TotalObj)End Sub
. . .
obj of objClass
. . .obj.UpdateTotal(amount). . .
obj of objClass
. . .obj.UpdateTotal(amount). . .
Class AutoResetEvent• Class ManualResetEvent
– Reset: change the state to non-signaled (Red Light)
– Set: change the state to signaled (Green light)
– WaitOne: wait for signal
– Like a traffic light
• Class AutoResetEvent
– (Reset): change the state to non-signaled (Red Light)
– State becomes non-signaled after a waiting thread released
– Set: change the state to signaled (Green light)
– WaitOne: wait for signal
– Like a Stop sign (combined with traffic light): one at a time19
20
Queue Class
• Represents a first-in, first-out collection of objects.
• This class implements a queue as a circular array.
• Public static (Shared in Visual Basic) members of this type are thread safe.
• Any instance members are not guaranteed to be thread safe.
21
Queue Constructor
Queue:
Initializes a new instance of the Queue class that is empty, has the default initial capacity, and uses the default growth factor.
Queue(Int32, Single):
Initializes a new instance of the Queue class that is empty, has the specified initial capacity, and uses the specified growth factor.
. . .
Queue MethodsPrivate myQueue As New Queue
' Puts obj at the end of the queue
' Any obj, could be a thread object
myQueue.Enqueue(obj)
' Takes the first obj from the queue
obj = myQueue.Dequeue
' Checks if the queue is empty
If myQueue.Count > 0 Then
' Points to the first obj of the queue
obj = myQueue.Peek
22
Multiple Threads Accessing One Same Queue
Any instance members are not guaranteed to be thread safe.
Private myQueue As New Queue
‘ To guarantee at most one thread accessing the queue Monitor.Enter(myQueue)
myQueue.Enqueue(obj)
' Could do other things
' Exits the queue waking up next waiting thread if any
Monitor.Exit(myQueue)
23
Multiple Threads Accessing One Same Queue
Monitor.Enter(myQueue)
' Checks if the queue is empty
If myQueue.Count > 0 Then
' Points to the first obj of the queue
obj = myQueue.Peek
If ... Then
obj = myQueue.Dequeue
. . .
End If
End If
Monitor.Exit(myQueue)24
25
Prog5: Readers and Writers
There is a data area shared among a number of threads
File, Memory, Registers, ...
Variable Total of Integer
Readers: Read data only
Writers: Modify data
Requirements:
Any number of Readers may simultaneously read the data.
A Writer requires exclusive access to the file.
First In First Out (FIFO) rule.
26
Sample Program
27
ReaderWriterLock
• Starvation
• Not a FIFO solution
• We don’t use the lock!
28
FIFO Solution• ReaderWriter Interface
– Reader Class – Writer Class
• Data Manager Class– Total
– FIFO Queue
– ReaderCount (RC)
• Number of readers reading the data
– WriterCount (WC)
• Number of writers writing the data
• Could be a Boolean
FIFO Solution
ReaderCount
0
29
WriterCount
0
ManagerManager is sleeping
Data
FIFO Solution
ReaderCount
0
30
WriterCount
0
R1
ManagerNew reader R1
R1 enters queue
R1 wakes up manager
R1 goes to sleep
Manager decides R1 can enter and read
(after checking RC, WC and queue)
Data
FIFO Solution
ReaderCount
0
31
WriterCount
0
R1
ManagerManager removes R1 from queue and wakes it up
Manager goes to sleep
R1 increments RC
R1 reads data
DataR1
1
FIFO Solution
ReaderCount
1
32
WriterCount
0
R2
ManagerNew reader R2
R2 enters queue
R2 wakes up manager
R2 goes to sleep
Manager decides R2 can enter and read
DataR1
FIFO Solution
ReaderCount
1
33
WriterCount
0
R2
ManagerManager removes R2 from queue and wakes it up
Manager goes to sleep
R2 increments RC
R2 reads data
DataR1R2
2
FIFO Solution
ReaderCount
2
34
WriterCount
0
W1
ManagerNew writer W1
W1 enters queue
W1 wakes up manager
W1 goes to sleep
Manager decides W1 has to wait
Manager goes to sleep
DataR1, R2
FIFO Solution
ReaderCount
2
35
WriterCount
0
W1
ManagerR1 finishes reading
R1 decrements RC
R1 leaves and done
DataR1, R2
1
DataR2
FIFO Solution
ReaderCount
1
36
WriterCount
0
W1 W2
ManagerNew writer W2
W2 enters queue
W2 wakes up manager
W2 goes to sleep
Manager decides W2 has to wait
DataR2
FIFO Solution
ReaderCount
1
37
WriterCount
0
W1 W2 R3
ManagerNew reader R3
R3 enters queue
R3 wakes up manager
R3 goes to sleep
Manager decides R3 has to wait
DataR2
FIFO Solution
ReaderCount
1
38
WriterCount
0
W1 W2 R3
ManagerR2 finishes reading
R2 decrements RC
R2 wakes up manager, since RC is 0
R2 leaves and done
Manager decides W1 can enter and write
0
DataR2
Data
FIFO Solution
ReaderCount
0
39
WriterCount
0
ManagerManager removes W1 from queue and wakes it up
Manager goes to sleep
W1 increments WC
W1 writes data
Data 1
W1 W2 R3
DataW1
W2 R3
FIFO Solution
ReaderCount
0
40
WriterCount
1
W2 R3 R4
ManagerNew reader R4
R4 enters queue
R4 wakes up manager
R4 goes to sleep
Manager does nothing and goes to sleep
DataW1
FIFO Solution
ReaderCount
0
41
WriterCount
1
W2 R3 R4 R5
ManagerNew reader R5
R5 enters queue
R5 wakes up manager
R5 goes to sleep
Manager does nothing and goes to sleep
DataW1
FIFO Solution
ReaderCount
0
42
WriterCount
1
W2 R3 R4 R5
ManagerW1 finishes writing
W1 decrements WC
W1 wakes up manager
W1 leaves and done
Manager decides W2 can enter and write
DataW1
0Data
FIFO Solution
ReaderCount
0
43
WriterCount
0
W2 R3 R4 R5
ManagerManager removes W2 from queue and wakes it up
Manager goes to sleep
W2 increments WC
W2 writes data
Data 1DataW2
R3 R4 R5
FIFO Solution
ReaderCount
0
44
WriterCount
1
ManagerNew writer W3
W3 enters queue
W3 wakes up manager
W3 goes to sleep
Manager does nothing
Manager goes to sleep
DataW2
R3 R4 R5 W3
FIFO Solution
ReaderCount
0
45
WriterCount
1
ManagerNew reader R6
R6 enters queue
R6 wakes up manager
R6 goes to sleep
Manager does nothing
Manager goes to sleep
DataW2
R3 R4 R5 W3 R6
FIFO Solution
ReaderCount
0
46
WriterCount
1
R3 R4 R5 W3 R6
Manager
W2 finishes writing
W2 decrements WC
W2 wakes up manager
W2 leaves and done
Manager decides all readers at beginning of queue can enter and read data
DataW2
0Data
FIFO Solution
ReaderCount
0
47
WriterCount
0
R3 R4 R5 W3 R6
Manager
Manager removes R3, R4, and R5 from queue and wakes them up (one at a time)
Manager goes to sleep
R3, R4, and R5 increment RC (one at a time)
R3, R4, and R5 read data
Data
3
DataR3, R4, R5
W3 R6
Data Manager ClassPublic Class ManagerClass
#Region "DataTypes"
Public Enum ReaderWriterType
Reader
Writer
End Enum
Public Enum State
Wait
Start
Finish
End Enum
Public Delegate Sub PassMessage(ByVal theID As String,
ByVal theState As State,
ByVal Total As Integer)
#End Region
48
Data Manager Class
Public Class ManagerClass
#Region "Data Members"
Private _total As Integer = 100
Private ReaderCount As Integer
Private WriterCount As Integer
Private DataObj As New Object
Private FIFOQueue As New Queue
Private _manager As Thread
Private _done As Boolean
Private ReaderWriterEvent As New AutoResetEvent(False)
Private endProgram As New AutoResetEvent(False)
#End Region
49
Data Manager ClassFriend Property TotalValue As Integer
Public Sub SpinUp
Public Sub SpinDown
Public Sub FinishReadWrite
Private Sub run()
‘ For Readers and Writers
Friend Sub WakeUpManager()
Friend Sub EnterFIFOQueue(ByVal obj As ReaderWriter)
Friend Sub UpdateCountsAndWakeupManager
(ByVal type As ReaderWriterType,
ByVal countDelta As Integer)
50
Data Manager Class‘ Called at the beginning to run the program
Public Sub SpinUp
‘ Called after the user answered Yes
‘ to terminate the program
Public Sub SpinDown
‘ Called after the user clicked Exit
‘ but before asking user Yes/No
Public Sub FinishReadWrite
51
Data Manager ClassPrivate Sub Run
While Not _done
Wait on ReaderWriterEvent
Lock the data object
Lock the queue
If Empty queue and no reading/writing
set endProgram
Else If some readers reading data
Let all readers at the beginning of queue in
Else If no writer writing data
Let first writer or all readers at beginning in
Else
Do not do any thing
Unlock the queue
Unlock the data object 52
Data Manager Class
‘ Must use Monitor to enforce mutual exclusion
Friend Sub EnterFIFOQueue(ByVal obj As ReaderWriter)
‘ Must use Monitor to enforce mutual exclusion
‘ countDelta is +1 or -1
‘ Reader will update ReaderCount
‘ and wakeup manager if ReaderCount is 0
‘ Writer will update WriterCount
‘ and wakeup manager if WriterCount is 0
Friend Sub UpdateCountsAndWakeupManager
(ByVal type As ReaderWriterType,
ByVal countDelta As Integer)
53
Interface ReaderWriter
‘ Could be an abstract class
Public Interface ReaderWriter
WriteOnly Property Manager() As ManagerClass
WriteOnly Property DisplayMsg() As ManagerClass.PassMessage
WriteOnly Property mainForm() As System.Windows.Forms.Form
ReadOnly Property ID() As String
ReadOnly Property type() As ManagerClass.ReaderWriterType
Sub SpinUp()
Sub WakeUp()
End Interface 54
Class ReaderPrivate readerThread As Thread
Private readerEvent As New AutoResetEvent(False)
Private generator As New Random()
‘Implement all methods of Interface ReaderWriter
WriteOnly Property Manager() As ManagerClass
WriteOnly Property DisplayMsg() As ManagerClass.PassMessage
WriteOnly Property mainForm() As System.Windows.Forms.Form
ReadOnly Property ID() As String
ReadOnly Property type() As ManagerClass.ReaderWriterType
Sub SpinUp()
Sub WakeUp()
55
Class ReaderImplement Interface ReaderWriter
Private readerThread As Thread
Private readerEvent As New AutoResetEvent(False)
Private generator As New Random()
56
Pseudo code for the Reader (For one reader and no loop inside: no spindown)
Entering the queue Display message Wakeup the manager and wait
Increment reader count Display message Read data
Decrement reader count Wakeup the manager if needed Display message
Class WriterPrivate writerThread As Thread
Private writerEvent As New ManualResetEvent(False)
Private generator As New Random(Now.Second)
‘Implement all methods of Interface ReaderWriter
WriteOnly Property Manager() As ManagerClass
WriteOnly Property DisplayMsg() As ManagerClass.PassMessage
WriteOnly Property mainForm() As System.Windows.Forms.Form
ReadOnly Property ID() As String
ReadOnly Property type() As ManagerClass.ReaderWriterType
Sub SpinUp()
Sub WakeUp()
57
Class WriterImplement Interface ReaderWriter
Private writerThread As Thread
Private writerEvent As New ManualResetEvent(False)
Private generator As New Random(Now.Second)
58
Pseudo code for the Writer(For one writer and no loop inside: no spindown)
Entering the queue Display message Wakeup the manager and wait
Increment writer count Display message Write data
Decrement writer count Wakeup the manager Display message
Using Interface
Private obj As ReaderWriter
obj = FIFOQueue.Peek
If obj.type = ReaderWriterType.Reader Then
...
Else
...
End If
59
EXIT Button on Main FormPrivate endProgThread As Thread
Private DBManager As New Manager
Private Sub btnExit_Click(...) Handles btnExit.Click
‘ Disable buttons
endProgThread = New Thread(AddressOf endProgThreadSub)
endProgThread.Start()
End Sub
Private Sub endProgThreadSub()
DBManager.FinishReadWrite()
‘ Ask user if to exit
If userAnswer = MsgBoxResult.Yes Then
DBManager.SpinDown()
Application.Exit()
Else
‘ Enable buttons: need a delegate!
End If
End Sub
60
Test 3 (40 points)
• When?
After Break
• What?
Threading
61
Project
After Break
VB Grader
Threads
Assembly
62