1 / 30

CS 3340

CS 3340. Windows Programming. Program 5. Due Friday, April 5. Test 3. Threads! Thursday, April 11. Prog4. user_thread . . . amount = … invoke delegate transact (amount). user_thread . . . amount = … invoke delegate transact (amount). user_thread . . . amount = …

garyking
Download Presentation

CS 3340

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 3340 Windows Programming

  2. Program 5 Due Friday, April 5

  3. Test 3 Threads! Thursday, April 11

  4. Prog4 user_thread . . . amount = … invoke delegate transact (amount) . . . user_thread . . . amount = … invoke delegate transact (amount) . . . user_thread . . . amount = … invoke delegate transact (amount) . . . mainThread . . . Total + = amount … Total

  5. Data Shared by Multiple Threads user_thread . . . amount = … Total += amount . . . user_thread . . . amount = … Total += amount . . . user_thread . . . amount = … Total += amount . . . Total

  6. Time Sharing threadOne threadTwo Time line The threads don’t know when the time out will occur.

  7. Data Shared by Multiple Threads user_thread . . . amount = … Total += amount // In assembly // Load Total // Add amount // Save Total . . . user_thread . . . amount = … Total += amount // In assembly // Load Total // Add amount // Save Total . . . Total

  8. 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 . . . user_thread Start running amount = -100 Total += -100 // In assembly // Load Total Total: 1000 // Add amount // Save Total Total: 900 . . . Total: 1000 900 1050

  9. Critical Section When accessing shared data. Should be executed as a single statement. Total += amount // Load Total // Add amount // Save Total

  10. Two Classes for Mutual Exclusion • MutEx • Monitor

  11. 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.

  12. 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.

  13. Class Monitor Features • It is associated with an object on demand. • It is unbound, which means it can be accessed directly from any context. • An instance of the Monitor class cannot be created. • Not inheritable Public NotInheritable Class Monitor • Shared methods

  14. 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.

  15. Monitor Methods • Enter • Exit • TryEnter • Wait • Pulse • PulseAll • . . .

  16. 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) . . .

  17. 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) . . . user_thread Start running amount = -100 Monitor.Enter(TotalObj) (wait here) (continue) (CriticalSection) Total += -100 // In assembly // Load Total Total: 1050 // Add amount // Save Total Total: 950 Monitor.Exit(TotalObj) . . . TotalObj Total: 1000 1050 950

  18. 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.

  19. 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. . . .

  20. Queue Methods Private myQueue As Queue = New Queue ' Puts obj at the end of the queue ' Any obj, could be a thread object (add in Java) myQueue.Enqueue(obj) ' Takes the first obj from the queue (remove in Java) 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

  21. Multiple Threads Accessing One Queue Any instance members are not guaranteed to be thread safe. Private myQueue As Queue = 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)

  22. Multiple Threads Accessing One 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)

  23. ManualResetEvent &AutoResetEvent • Class ManualResetEvent • Set: change the state to signaled (Green light) • WaitOne: wait for signal • Reset: change the state to non-signaled (Red Light) • Like a traffic light • Class AutoResetEvent • Set: change the state to signaled (Green light) • WaitOne: wait for signal • State becomes non-signaled after a waiting thread released • Like a Stop sign (combined with traffic light): one at a time • (Reset): change the state to non-signaled (Red Light)

  24. Prog5: Readers and Writers There are some data items 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 data. First In First Out (FIFO) rule.

  25. ReaderWriterLock • Starvation • Not a FIFO solution • We don’t use the lock!

  26. FIFO Solution • DatabaseClass • Total • ReaderCount (RC) • Number of readers reading the data • WriterCount (WC) • Number of writers writing the data • Could be a Boolean • DataObj • To use Monitor on Total, RC and WC • Not thread Safe!

  27. FIFO Solution • ReaderWriter Class • FIFO Queue • ReaderWriterEvent: AutoResetEvent • To wait in the FIFO queue • EndProgram: AutoResetEvent • To terminate the program • ID • Type • Sub Run

  28. FIFO Solution • Reader class and Writer Class • Sub-class of ReaderWriter • Overrides ID • Overrides Type • Overrides Run • Mutual exclusion! • FIFO! • No Deadlock! No Starvation!

  29. Pseudo code for the Run method of Reader (For one reader and no loop inside: no spindown) If the queue is not empty or the database status is writing Add itself to the queue Wait for _ReaderWriterEvent Wakeup next reader before accessing the database Increase reader count by one Reading the value Decrease reader count by one If the database status is empty Wakeup next one in the queue

  30. Pseudo code for the Run method of Writer (For one writer and no loop inside: no spindown) If the queue is not empty or the database status is not empty Add itself to the queue Wait for _ReaderWriterEvent (Wakeup next reader before accessing the database) Increase writer count by one Update the value Decrease writer count by one (If the database status is empty) Wakeup next one in the queue

More Related