Download presentation
Presentation is loading. Please wait.
Published byHector Griffith Modified over 9 years ago
1
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
2 Data Shared by Multiple Threads user_thread... amount = … Total += amount... user_thread... amount = … Total += amount... user_thread... amount = … Total += amount... Total
3
3 Time Sharing threadOne threadTwo Time line The threads don’t know when the time out will occur.
4
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
5 Mutual Exclusion user_thread Start running amount = 50 Total += 50 // In assembly // Load Total Total: 1000 Time Out Continue running Total: 1000 // Add amount // Save Total Total: 1050... Total: 1000 900 1050 user_thread Start running amount = -100 Total += -100 // In assembly // Load Total Total: 1000 // Add amount // Save Total Total: 900...
6
Critical Section When accessing shared data. Should be executed as a single statement. Total += amount // Load Total // Add amount // Save Total 6
7
Two Classes for Mutual Exclusion MutEx Monitor 7
8
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
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
10 Using MutEx Object user_thread Start running amount = 50 MutExObj.WaitOne (CriticalSection) Total += 50 // In assembly // Load Total Total: 1000 Time Out Continue running Total: 1000 // Add amount // Save Total Total: 1050 MutExObj.Release... MutExObj Total: 1000 1050 950 user_thread Start running amount = -100 MutExObj.WaitOne (CriticalSection) Total += -100 // In assembly // Load Total Total: 1050 // Add amount // Save Total Total: 950 MutExObj.Release...
11
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
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
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
14 Monitor Methods Enter Exit TryEnter Wait Pulse PulseAll...
15
15 Using Class Monitor Private Total As Integer ‘ Cannot use Monitor on integers Private TotalObj As New Object user_thread... amount = 50 Monitor.Enter(TotalObj) (CriticalSection) Total += 50 // In assembly // Load Total // Add amount // Save Total Monitor.Exit(TotalObj)...
16
16 Using Class Monitor user_thread Start running amount = 50 Monitor.Enter(TotalObj) (CriticalSection) Total += 50 // In assembly // Load Total Total: 1000 Time Out Continue running Total: 1000 // Add amount // Save Total Total: 1050 Monitor.Exit(TotalObj)... TotalObj Total: 1000 1050 950 user_thread Start running amount = -100 Monitor.Enter(TotalObj) (CriticalSection) Total += -100 // In assembly // Load Total Total: 1050 // Add amount // Save Total Total: 950 Monitor.Exit(TotalObj)...
17
17 Using Monitor in Class Method Public 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 Sub End Class
18
18 Using Monitor in Class Method obj of objClass... obj.UpdateTotal(amount)... ManagerClass Private Total As Integer Private 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)...
19
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 time 19
20
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
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....
22
Queue Methods Private 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
23
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
24
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 Monitor.Exit(myQueue) 24
25
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
26 Sample Program
27
27 ReaderWriterLock Starvation Not a FIFO solution We don’t use the lock!
28
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
29
FIFO Solution ReaderCount 0 29 WriterCount 0 Manager Manager is sleeping Data
30
FIFO Solution ReaderCount 0 30 WriterCount 0 R1 Manager New 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
31
FIFO Solution ReaderCount 0 31 WriterCount 0 R1 Manager Manager removes R1 from queue and wakes it up Manager goes to sleep R1 increments RC R1 reads data Data R1 1
32
FIFO Solution ReaderCount 1 32 WriterCount 0 R2 Manager New reader R2 R2 enters queue R2 wakes up manager R2 goes to sleep Manager decides R2 can enter and read Data R1
33
FIFO Solution ReaderCount 1 33 WriterCount 0 R2 Manager Manager removes R2 from queue and wakes it up Manager goes to sleep R2 increments RC R2 reads data Data R1 R2 2
34
FIFO Solution ReaderCount 2 34 WriterCount 0 W1 Manager New writer W1 W1 enters queue W1 wakes up manager W1 goes to sleep Manager decides W1 has to wait Manager goes to sleep Data R1, R2
35
FIFO Solution ReaderCount 2 35 WriterCount 0 W1 Manager R1 finishes reading R1 decrements RC R1 leaves and done Data R1, R2 1 Data R2
36
FIFO Solution ReaderCount 1 36 WriterCount 0 W1W2 Manager New writer W2 W2 enters queue W2 wakes up manager W2 goes to sleep Manager decides W2 has to wait Data R2
37
FIFO Solution ReaderCount 1 37 WriterCount 0 W1W2R3 Manager New reader R3 R3 enters queue R3 wakes up manager R3 goes to sleep Manager decides R3 has to wait Data R2
38
FIFO Solution ReaderCount 1 38 WriterCount 0 W1W2R3 Manager R2 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 Data R2 Data
39
FIFO Solution ReaderCount 0 39 WriterCount 0 Manager Manager removes W1 from queue and wakes it up Manager goes to sleep W1 increments WC W1 writes data Data 1 W1W2R3 Data W1 W2R3
40
FIFO Solution ReaderCount 0 40 WriterCount 1 W2R3R4 Manager New reader R4 R4 enters queue R4 wakes up manager R4 goes to sleep Manager does nothing and goes to sleep Data W1
41
FIFO Solution ReaderCount 0 41 WriterCount 1 W2R3R4R5 Manager New reader R5 R5 enters queue R5 wakes up manager R5 goes to sleep Manager does nothing and goes to sleep Data W1
42
FIFO Solution ReaderCount 0 42 WriterCount 1 W2R3R4R5 Manager W1 finishes writing W1 decrements WC W1 wakes up manager W1 leaves and done Manager decides W2 can enter and write Data W1 0 Data
43
FIFO Solution ReaderCount 0 43 WriterCount 0 W2R3R4R5 Manager Manager removes W2 from queue and wakes it up Manager goes to sleep W2 increments WC W2 writes data Data 1 W2 R3R4R5
44
FIFO Solution ReaderCount 0 44 WriterCount 1 Manager New writer W3 W3 enters queue W3 wakes up manager W3 goes to sleep Manager does nothing Manager goes to sleep Data W2 R3R4R5W3
45
FIFO Solution ReaderCount 0 45 WriterCount 1 Manager New reader R6 R6 enters queue R6 wakes up manager R6 goes to sleep Manager does nothing Manager goes to sleep Data W2 R3R4R5W3R6
46
FIFO Solution ReaderCount 0 46 WriterCount 1 R3R4R5W3R6 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 Data W2 0 Data
47
FIFO Solution ReaderCount 0 47 WriterCount 0 R3R4R5W3R6 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 R3, R4, R5 W3R6
48
Data Manager Class Public 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
49
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
50
Data Manager Class Friend 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
51
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
52
Data Manager Class Private 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
53
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
54
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
55
Class Reader Private 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
56
Class Reader Implement 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
57
Class Writer Private 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
58
Class Writer Implement 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
59
Using Interface Private obj As ReaderWriter obj = FIFOQueue.Peek If obj.type = ReaderWriterType.Reader Then... Else... End If 59
60
EXIT Button on Main Form Private 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
61
Test 3 (40 points) When? After Break What? Threading 61
62
Project After Break VB Grader Threads Assembly 62
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.