Atomicity CS 2110 Fall 2017

Similar documents
Synchronization Spinlocks - Semaphores

Synchronization Lecture 24 Fall 2018

Synchronization COMPSCI 386

Agenda. Highlight issues with multi threaded programming Introduce thread synchronization primitives Introduce thread safe collections

Last Class: Synchronization

Synchronization. Announcements. Concurrent Programs. Race Conditions. Race Conditions 11/9/17. Purpose of this lecture. A8 released today, Due: 11/21

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Synchronization II: EventBarrier, Monitor, and a Semaphore. COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala

More thread safety and Concurrent collections. Nasser Giacaman CompSci 230: Software Design and Construction

Synchronization Lecture 23 Fall 2017

Atomic Variables & Nonblocking Synchronization

Synchronization for Concurrent Tasks

CS 179: GPU Programming LECTURE 5: GPU COMPUTE ARCHITECTURE FOR THE LAST TIME

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

Lesson 6: Process Synchronization

Programming Languages

Results of prelim 2 on Piazza

Synchronization in Concurrent Programming. Amit Gupta

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (II)

Process/Thread Synchronization

CROWDMARK. Examination Midterm. Spring 2017 CS 350. Closed Book. Page 1 of 30. University of Waterloo CS350 Midterm Examination.

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

Introduction to Locks. Intrinsic Locks

CS 5523 Operating Systems: Midterm II - reivew Instructor: Dr. Tongping Liu Department Computer Science The University of Texas at San Antonio

Threads Synchronization

Lecture 8: September 30

Problems with Concurrency. February 19, 2014

Models of concurrency & synchronization algorithms

Process/Thread Synchronization

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

Synchronization. Before We Begin. Synchronization. Credit/Debit Problem: Race Condition. CSE 120: Principles of Operating Systems.

Synchronization. Before We Begin. Synchronization. Example of a Race Condition. CSE 120: Principles of Operating Systems. Lecture 4.

Synchronization Principles

Chapter 6: Process Synchronization

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

Chapter 7: Process Synchronization!

Programming in Parallel COMP755

Advanced MEIC. (Lesson #18)

What is a thread anyway?

Process Synchronization (Part I)

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

Java Threads Vs Processes. CS Concurrent Programming. Java Threads. What can a thread do? Java Concurrency

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004

CSE 120: Principles of Operating Systems. Lecture 4. Synchronization. October 7, Prof. Joe Pasquale

Synchronising Threads

CS 450 Exam 2 Mon. 4/11/2016

Chapter 6: Process Synchronization. Module 6: Process Synchronization

CS370 Operating Systems

Principles of Concurrent Programming TDA384/DIT391

CS420: Operating Systems. Process Synchronization

Introduction to OS Synchronization MOS 2.3

Multithreading and Interactive Programs

Synchronization. Disclaimer: some slides are adopted from the book authors slides with permission 1

Introduction to Lock-Free Programming. Olivier Goffart

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming.

Process Synchronization

Achieving Synchronization or How to Build a Semaphore

CS 153 Design of Operating Systems Winter 2016

10/17/2011. Cooperating Processes. Synchronization 1. Example: Producer Consumer (3) Example

IV. Process Synchronisation

The University of Texas at Arlington

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

thread thread thread thread atomic class J2SE 5.0 class atomic class condition class race condition threaded lock lock

THREADS: (abstract CPUs)

Part II Process Management Chapter 6: Process Synchronization

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Module 6: Process Synchronization. Operating System Concepts with Java 8 th Edition

Chapter 6: Process Synchronization

Synchronization I. Jo, Heeseung

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Verifying Concurrent Programs

Process Synchronization

Today: Synchronization. Recap: Synchronization

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

CS4411 Intro. to Operating Systems Exam 1 Fall points 10 pages

G52CON: Concepts of Concurrency

Concept of a process

Sharing Objects Ch. 3

Threads and Java Memory Model

Concurrency: a crash course

CS 134: Operating Systems

Distributed Computing Group

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

The New Java Technology Memory Model

CS5460: Operating Systems

M4 Parallelism. Implementation of Locks Cache Coherence

Agenda. Lecture. Next discussion papers. Bottom-up motivation Shared memory primitives Shared memory synchronization Barriers and locks

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

Parallel Programming Languages COMP360

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

An Introduction to Parallel Systems

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Review: Easy Piece 1

Transcription:

Atomicity CS 2110 Fall 2017

Parallel Programming Thus Far Parallel programs can be faster and more efficient Problem: race conditions Solution: synchronization Are there more efficient ways to ensure the correctness of parallel programs?

Selling Widgets public class WidgetStore{ private int numwidgets; /** produce widgets */ public void produce(){ /** sell all the widgets */ public void sell(){ sell() display() /** diplay widget if there /* are any available */ public void display(){ display() might continue displaying widgets after all the widgets are sold!!!

Caching Data is stored in caches: small, fast storage units Only written to main memory occasionally Huge efficiency gains! Each CPU has its own cache Each thread maintains its own cache entries Huge concurrency headaches!

keyword volatile public class WidgetStore{ private volatile int numwidgets; /** produce widgets */ public void produce(){ /** sell all the widgets */ public void sell(){ /** diplay widget if there /* are any available */ public void display(){ Variables declared as volatile will not be stored in the cache. All writes will write directly to main memory. All reads will read directly from main memory.

Handling Writes int numwidgets = 0; Thread 1(produce) numwidgets++; Thread 2 (sell) numwidgets- - ; What is the value of x? Can be either -1, 0, or 1!

Handling Writes volatile int numwidgets = 0; Thread 1(produce) numwidgets++; Thread 2 (sell) numwidgets- - ; What is the value of x? Can be either -1, 0, or 1!

The Problem with Writes Thread 1 Thread 2 Initially, i = 0 tmp = load i; Load 0 from memory Load 0 from memory tmp = load i; tmp = tmp + 1; store tmp to i; Store 1 to memory Store 1 to memory tmp = tmp - 1; store tmp to i; time Finally, i = -1

Concurrent Writes Solution 1: synchronized private int numwidgets; public void produce(){... synchronized(this){ numwidgets++;... It works But locks can be slow Solution 2: atomic values private AtomicInteger numwidgets; public void produce(){... synchronized(this){ numwidgets++;... Less powerful More efficient

Atomic Values Package java.util.concurrent.atomic defines a toolkit of classes that implement atomic values atomic values support lock-free, thread-safe programming on single variables class AtomicInteger, AtomicReference<E>, Atomic values extend the idea of volatile method get(): reads current value like volatile method set(newvalue): writes value like volatile implements new atomic operations

Compare and Set (CAS) 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 as hardware instructions does not use 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 that do this public int incrementandget() public int addandget(int delta) public int updateandget(inunaryoperator updatefunction)

Locks with CAS public class WidgetStore{ private int numwidgets; /** produce widgets */ public synchronized void produce(){ public class WidgetStore{ private int numwidgets; private boolean lock; /** produce widgets */ public synchronized void produce(){ while(!lock.compareandset(false, true)){ lock = false;

Lock-Free Data Structures Usable by many concurrent threads using only atomic actions no locks! compare and swap is your best friend but it only atomically updates one variable at a time! Let s look at one! Lock-free binary search tree [Ellen et al., 2010] http://www.cs.vu.nl//~tcs/cm/cds/ellen.pdf

More Concurrency Concurrency is actually an OS-level concern Different platforms have different concurrency APIs Programming languages provide abstractions There are lots of techniques for write concurrent programs lock (e.g., synchronized), mutex atomic operations semaphores condition variables transactional memory