Concurrency 2 CS 2110 – Spring 2016.

Slides:



Advertisements
Similar presentations
Computer Science 320 Clumping in Parallel Java. Sequential vs Parallel Program Initial setup Execute the computation Clean up Initial setup Create a parallel.
Advertisements

50.003: Elements of Software Construction Week 6 Thread Safety and Synchronization.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Concurrency 101 Shared state. Part 1: General Concepts 2.
Parallel Processing (CS526) Spring 2012(Week 6).  A parallel algorithm is a group of partitioned tasks that work with each other to solve a large problem.
Week 9, Class 3: Model-View-Controller Final Project Worth 2 labs Happens-Before ( SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
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.
1 What is the “volatile” Keyword? You can skip locking (thread synchronization) in some cases by using the volatile variables. –No locking/unlocking =
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Understanding class definitions Looking inside classes.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
CS116 Tutorial 4 Mutation. Review set! begin equal? vs. eq? Memory diagrams.
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
New Tools And Workshop Mod & For Loops. Modulo Calculates the remainder (remember long division?) % Examples: 7 % 3 10 % 2 2 % 3 evaluates to 1 evaluates.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
1 Object Oriented Programming Lecture XII Multithreading in Java, A few words about AWT and Swing, The composite design pattern.
Sharing Objects  Synchronization  Atomicity  Specifying critical sections  Memory visibility  One thread’s modification seen by the other  Visibility.
Java Thread and Memory Model
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Multiprocessor Cache Consistency (or, what does volatile mean?) Andrew Whitaker CSE451.
December 1, 2006©2006 Craig Zilles1 Threads and Cache Coherence in Hardware  Previously, we introduced multi-cores. —Today we’ll look at issues related.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CS 2200 Presentation 18b MUTEX. Questions? Our Road Map Processor Networking Parallel Systems I/O Subsystem Memory Hierarchy.
Week 9, Class 3: Java’s Happens-Before Memory Model (Slides used and skipped in class) SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
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.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Week 8, Class 3: Model-View-Controller Final Project Worth 2 labs Cleanup of Ducks Reducing coupling Finishing FactoryMethod Cleanup of Singleton SE-2811.
Concurrency Idea. 2 Concurrency idea Challenge –Print primes from 1 to Given –Ten-processor multiprocessor –One thread per processor Goal –Get ten-fold.
December 1, 2006©2006 Craig Zilles1 Threads & Atomic Operations in Hardware  Previously, we introduced multi-core parallelism & cache coherence —Today.
Java Thread Programming
Concurrency 3 CS 2110 – Spring 2017.
Threads Cannot Be Implemented As a Library
6/16/2010 Parallel Performance Parallel Performance.
Atomic Operations in Hardware
Atomic Operations in Hardware
Atomicity CS 2110 – Fall 2017.
Threads, Concurrency, and Parallelism
Threads and Memory Models Hal Perkins Autumn 2011
Monitors Chapter 7.
null, true, and false are also reserved.
Designing Parallel Algorithms (Synchronization)
An Introduction to Java – Part I, language basics
The Boolean (logical) data type boolean
Lecture Notes – Week 3 Lecture-2
The C++ Memory model Implementing synchronization)
Implementing synchronization
Threads and Memory Models Hal Perkins Autumn 2009
Critical section problem
Grades.
Monitors Chapter 7.
Chapter 6: Process Synchronization
CSCI1600: Embedded and Real Time Software
Monitors Chapter 7.
Why we have Counterintuitive Memory Models
Chapter 6: Synchronization Tools
CSCI1600: Embedded and Real Time Software
Problems with Locks Andrew Whitaker CSE451.
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
Parallel Programming Exercise Session 11
Threads and concurrency / Safety
Presentation transcript:

Concurrency 2 CS 2110 – Spring 2016

Consistency What is printed? x = 1; y = -1; x = 2; y = 3; Thread 1 Thread 2 x = 2; y = 3; a = y > 0 ? x : 0; System.out.println(a); What is printed? 0, 1, and 2 can be printed!

Not sequentially consistent! Consistency Thread 1 on Core 1 Thread 2 on Core 2 Write 2 to x in local cache Write 3 to y in local cache 3 gets pushed to y in memory 2 gets pushed to x in memory Read 3 from y in memory Read 1 from x in memory Write 1 to a Print 1 Not sequentially consistent!

Harsh Reality Sequential Consistency Volatile keyword There is an interleaving of the parallel operations that explains the observations and events Currently unknown how to implement efficiently Volatile keyword Java fields can be declared volatile Writing to a volatile variable ensures all local changes are made visible to other threads x and y would have to be made volatile to fix code

Atomicity What is the value of x? volatile int x = 0; x++; x++; volatile does not ensure atomicity! volatile int x = 0; Thread 1 Thread 2 x++; x++; What is the value of x? Can be both 1 and 2!

java.util.concurrent.atomic class AtomicInteger, AtomicReference<T>, … Represents a value method set(newValue) has the effect of writing to a volatile variable method get() returns the current value effectively an extension of volatile but what about atomicity???

Compare and Set (CAS) AtomicInteger n = new AtomicInteger(5); boolean compareAndSet(expectedValue, newValue) If value doesn’t equal expectedValue, return false if equal, store newValue in value and return true executes as a single atomic action! supported by many processors without requiring locks! AtomicInteger n = new AtomicInteger(5); n.compareAndSet(3, 6); // return false – no change n.compareAndSet(5, 7); // returns true – now is 7

Incrementing with CAS /** Increment n by one. Other threads use n too. */ public static void increment(AtomicInteger n) { int i = n.get(); while (n.compareAndSet(i, i+1)) i = n.get(); } // AtomicInteger has increment methods doing this

Lock-Free Data Structures Usable by many concurrent threads using only atomic actions – no locks! compare and swap is god here but it only atomically updates one variable at a time! Let’s implement one!