Threading Part 3 CS221 – 4/24/09. Teacher Survey Fill out the survey in next week’s lab You will be asked to assess: – The Course – The Teacher – The.

Slides:



Advertisements
Similar presentations
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Advertisements

– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
50.003: Elements of Software Construction Week 6 Thread Safety and Synchronization.
Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 24 th, 2010 The University of Georgia.
CMSC 330: Organization of Programming Languages Threads Classic Concurrency Problems.
Concurrency 101 Shared state. Part 1: General Concepts 2.
Chapter 3 The Critical Section Problem
CY2003 Computer Systems Lecture 05 Semaphores - Theory.
CS 11 java track: lecture 7 This week: Web tutorial:
CS 162 Discussion Section Week 3. Who am I? Mosharaf Chowdhury Office 651 Soda 4-5PM.
Threading Part 4 CS221 – 4/27/09. The Final Date: 5/7 Time: 6pm Duration: 1hr 50mins Location: EPS 103 Bring: 1 sheet of paper, filled both sides with.
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
Threads Load new page Page is loading Browser still responds to user (can read pages in other tabs)
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
Assignment – no class Wednesday All: watch the Google Techtalk “Getting C++ Threads Right” by Hans Boehm at the following link in place of Wednesday’s.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
29-Jun-15 Java Concurrency. Definitions Parallel processes—two or more Threads are running simultaneously, on different cores (processors), in the same.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
1 Outline Processes Threads Inter-process communication (IPC) Classical IPC problems Scheduling.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 19 th, 2010 The University of Georgia.
Week 9 Building blocks.
Nachos Phase 1 Code -Hints and Comments
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
50.003: Elements of Software Construction Week 8 Composing Thread-safe Objects.
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
Optimistic Design 1. Guarded Methods Do something based on the fact that one or more objects have particular states  Make a set of purchases assuming.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Producer-Consumer Problem The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue.bufferqueue.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Sharing Objects  Synchronization  Atomicity  Specifying critical sections  Memory visibility  One thread’s modification seen by the other  Visibility.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Synchronizing Threads with Semaphores
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Optimistic Design CDP 1. Guarded Methods Do something based on the fact that one or more objects have particular states Make a set of purchases assuming.
Semaphores Reference –text: Tanenbaum ch
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Lecture 5 Page 1 CS 111 Summer 2013 Bounded Buffers A higher level abstraction than shared domains or simple messages But not quite as high level as RPC.
Race Conditions & Synchronization
Race Conditions & Synchronization
Semaphores Reference text: Tanenbaum ch
Background on the need for Synchronization
Interprocess Communication (3)
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Synchronization Lecture 23 – Fall 2017.
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Race Conditions & Synchronization
Producer-Consumer Problem
Coding Concepts (Basics)
Dr. Mustafa Cem Kasapbaşı
Semaphores Reference text: Tanenbaum ch
Threads and concurrency / Safety
Synchronization and liveness
Presentation transcript:

Threading Part 3 CS221 – 4/24/09

Teacher Survey Fill out the survey in next week’s lab You will be asked to assess: – The Course – The Teacher – The TA To login you’ll only need your last name and banner ID

Where We Left Off Data integrity options: – Synchronized methods – Synchronized statements using locks – Atomic data access – Immutable objects Order of operations options: – Guarded blocks – Locks

Atomic Variables The problem: – We want C++ to be atomic – We don’t want to block other threads with synchronized method or statement – Volatile doesn’t work except in the simplest scenarios The solution: – Atomic Variable

Atomic Variables Look in Java.Util.Concurrent.Atomic Note there are a variety of atomic types: – Boolean – Integer – IntegerArray – Long – LongArray – Etc.

Atomic Variables Atomic variables support lock-free thread-safe data access and modification. Basically these are volatile + they support get and update atomically On increment and decrement, Volatile failed us Atomic variables should solve the problem

Example Let’s look at Volatile vs. Synchronized vs. AtomicInteger

Immutable Objects An immutable object cannot be changed once it is constructed Examples – String class – Integer class Immutable objects are automatically thread- safe

Immutable Objects In order to change an immutable object – Create a new instance – Copy the new value into the new instance – GC eventually cleans up the old instance

Immutable Objects Pros – Don’t have the overhead of thread synchronization. Improved performance, reduced complexity – Generally simple and robust Cons – If it changes you need to create a new instance. Minor performance impact. – Not a good choice for storing sensitive data

How to Create an Immutable Class Don’t provide setter methods Make all fields final and private Don’t allow subclasses to override methods – E.g. Make the class final Don’t provide any methods that modify mutable objects Don’t pass references to mutable objects back to the caller

Example

Multi-threaded Problems Looking at this code… …what problems can you foresee?

Multi-threaded Problems Thread 1: Color color(255, 255, 255, “White”); int myColorInt = color.getRGB(); String myColorName = color.getName(); Thread 2: color.set(0, 0, 0, “Black”);

Convert to Immutable There are two setters in the class – Set() – Invert() Remove Set, no need in an immutable class Change Invert to return a copy

Convert to Immutable There are a set of private fields Make them final as well

Convert to Immutable We want to make sure the class can’t be overridden Make the class final

Immutable RGB

Thread Synchronization So far we’ve talked about ways to protect data integrity What if you want to ensure correct order of operations? – E.g. Thread 1 cannot do X until Thread 2 does Y

Producer-Consumer Problem Producer-Consumer is an example where order of operation thread synchronization is important This is a classic multi-threaded synchronization problem Imagine two threads: – Producer, creates data – Consumer, does something with that data

Producer-Consumer Problem Furthermore: – Producer and consumer share a fixed size buffer – Producer generates a piece of data, sticks it in the buffer, repeats – Consumer pulls one piece of data at a time from the buffer – Producer shouldn’t try to add if the buffer is full – Consumer should try to remove from the buffer if it is empty

Producer-Consumer Problem Therefore: – Producer should sleep if the buffer is full and only wake up after consumer has removed an item – Consumer should sleep if the buffer is empty and only wake up after the producer has added an item

Simple Solution

What’s the Problem?

The Problem Consumer notices there are no items and sleeps Producer adds an item and wakes up the consumer The consumer is not yet fully asleep so the wakeup call is lost Result is Deadlock! – Consumer sleeps forever – Producer fills the buffer and then sleeps forever

Liveness This is the problem of liveness It is the classic problem of thread run-time synchronization How do you coordinate two threads without getting stuck?

Liveness Problems Can be broken into three categories: – Deadlock – Starvation – Livelock

Deadlock Two or more threads are blocked on each other, waiting forever.

Example

Starvation A thread needs access to a resource but cannot get it for long periods of time Caused by a greedy thread which blocks other threads access to the resource For instance – Imagine a synchronized method that is very slow to return – Thread 1 calls this method often – Thread 2, when calling the method, will often be blocked

Livelock Thread 1 takes action in response to Thread 2 Thread 2 takes action in response to Thread 1 Threads aren’t blocked but they are in an endless loop of responses and won’t do other work.

Livelock Example Alphonse moves to his left to let Gaston pass, while Gaston moves to his right to let Alphonse pass. Seeing that they are still blocking each other, Alphone moves to his right, while Gaston moves to his left. They're still blocking each other, so...

Guarded Blocks In order to coordinate activity between two threads we can use Guarded Blocks Wait() – puts a thread to sleep until it is notifie Notify() – wakes up one thread that is waiting on this object NotifyAll() – wakes up all threads waiting on this object

Guarded Blocks Example

Fixing the Deadlock How would we fix the earlier bowing deadlock?

Producer-Consumer Example Let’s look at a more complex example Remember the producer-consumer scenario? Our implementation is inefficient. Why? How do we improve it?

Producer-Consumer Example