Threads Synchronization

Similar documents
COMP 346 WINTER Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION

Faculty of Computers & Information Computer Science Department

ECE 462 Object-Oriented Programming using C++ and Java. Scheduling and Critical Section

Concurrency in Object Oriented Programs 1. Object-Oriented Software Development COMP4001 CSE UNSW Sydney Lecturer: John Potter

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

Java s Implementation of Concurrency, and how to use it in our applications.

Synchronization COMPSCI 386

CS 159: Parallel Processing

Lecture 8: September 30

Concurrency: a crash course

Introduction to Locks. Intrinsic Locks

CSE332: Data Abstractions Lecture 19: Mutual Exclusion and Locking

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

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

Threads and Parallelism in Java

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

Synchronization Spinlocks - Semaphores

3/25/14. Lecture 25: Concurrency. + Today. n Reading. n P&C Section 6. n Objectives. n Concurrency

CSE 332: Locks and Deadlocks. Richard Anderson, Steve Seitz Winter 2014

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

Synchronization in Concurrent Programming. Amit Gupta

Dealing with Issues for Interprocess Communication

Only one thread can own a specific monitor

+ Today. Lecture 26: Concurrency 3/31/14. n Reading. n Objectives. n Announcements. n P&C Section 7. n Race conditions.

THREADS: (abstract CPUs)

Atomicity CS 2110 Fall 2017

1 of 6 Lecture 7: March 4. CISC 879 Software Support for Multicore Architectures Spring Lecture 7: March 4, 2008

IV. Process Synchronisation

What's wrong with Semaphores?

Multiple Inheritance. Computer object can be viewed as

CMSC421: Principles of Operating Systems

Process Synchronization

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Monitors; Software Transactional Memory

Synchronization in Java

Sharing is the Key. Lecture 25: Parallelism. Canonical Example. Bad Interleavings. Common to have: CS 62 Fall 2016 Kim Bruce & Peter Mawhorter

Parallel Programming Languages COMP360

Recapitulation of basics. Concurrent Programming in Java. Java Threads. Managing Thread objects. Concurrent Thread Execution. Starting a Thread (1)

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

CSC 1600: Chapter 6. Synchronizing Threads. Semaphores " Review: Multi-Threaded Processes"

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Programming Languages

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

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Synchronization I. Jo, Heeseung

CS 153 Lab4 and 5. Kishore Kumar Pusukuri. Kishore Kumar Pusukuri CS 153 Lab4 and 5

The Problem with Threads

CS420: Operating Systems. Process Synchronization

Multithreaded Programming Part II. CSE 219 Stony Brook University, Department of Computer Science

Programming in Parallel COMP755

IT 540 Operating Systems ECE519 Advanced Operating Systems

Monitors; Software Transactional Memory

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

CS 351 Design of Large Programs Threads and Concurrency

Lecture 29: Java s synchronized statement

Multi-threaded programming in Java

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

Last Class: Synchronization

Chapter 6: Process Synchronization

Associate Professor Dr. Raed Ibraheem Hamed

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

CMSC 330: Organization of Programming Languages

Lecture 5: Synchronization w/locks

C09: Process Synchronization

Shared Objects & Mutual Exclusion

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack

G52CON: Concepts of Concurrency

Synchronising Threads

Concurrent Programming using Threads

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

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

Performance Throughput Utilization of system resources

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

Chapter 6 Process Synchronization

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

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Concurrent Programming Lecture 10

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

CS370 Operating Systems

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 5 Programming with Locks and Critical Sections

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Java Threads and intrinsic locks

CSE 332: Data Structures & Parallelism Lecture 17: Shared-Memory Concurrency & Mutual Exclusion. Ruth Anderson Winter 2019

Techniques of Java Programming: Concurrent Programming in Java

CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections. Tyler Robison Summer 2010

The University of Texas at Arlington

Advanced Concepts of Programming

Concurrency in Java Prof. Stephen A. Edwards

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

Threads. Threads The Thread Model (1) CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5

EE458 - Embedded Systems Lecture 8 Semaphores

System Software Assignment 8 Deadlocks

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

EECE.4810/EECE.5730: Operating Systems Spring 2017 Homework 2 Solution

High Performance Computing Course Notes Shared Memory Parallel Programming

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

Discussion CSE 224. Week 4

Global Environment Model

Transcription:

Synchronization

Threads Synchronization Threads share memory so communication can be based on shared references. This is a very effective way to communicate but is prone to two types of errors: Interference between threads Memory inconsistency errors Programação Concorrente e Distribuída 2

Stack of integers class Pilha { private int pos = 0; private int pilha[]= new int[100]; public void push(int i) { pos++; pilha[pos]=i; public int pop() { if (pos>0){ return(pilha[pos--]); else return 0; Non atomic operation (3 steps) 1. Memory read of pos 2. Increment of pos 3. Memory store of pos public int peak() { return pilha[pos]; Programação Concorrente e Distribuída 3

Stack of integers class Pilha { private int pos = 0; private int pilha[]= new int[100]; public void push(int i) { pos++; pilha[pos]=i; If two threads (A and B) both do a push on the same stack at the same time. We could have: A) pos++ (pos<-1) B) pos++ (pos<-2) A) pilha[pos]=7 B) pilha[pos]=4 The incoherent result would be: - In position 1 there is a meaningless value. - In position 2 the value 4 is stort.ed. - The value 7 from thread A is los Programação Concorrente e Distribuída 4

Thread Interference Interference can happen when the actions of two or more threads on the same data are not atomic. Non-atomic actions can be divided into a set of atomic actions. This means that atomic actions can be interleaved or parallelized and can therefore access to the data in an intermediate / incomplete state. Programação Concorrente e Distribuída 5

Memory inconsistency errors Happens when two or more threads have different versions of the same data. Thread A) pos++ Thread B) a=pos; What are the possible outcomes? Programação Concorrente e Distribuída 6

Memory inconsistency errors Thread A) pos++ Thread B) a=pos; The result depends on the order of the access to the data (pos). It's important to establish an order in the execution of different actions of different threads Programação Concorrente e Distribuída 7

Synchronization and atomic actions Atomic actions: Instructions that can be executed by a single thread at any time only. Critical section: A sequence of instructions that have to be executed atomically. Synchronized: A method or a section of Java code that should be treated as a critical section and therefore should be executing atomically. Programação Concorrente e Distribuída 8

How should the synchronized mechanism work? When entering a synchronized method, all other threads are automatically suspended... This solution is not efficient because a method can be complex and take a long time, and even threads that would never interfere would also be suspended. Programação Concorrente e Distribuída 9

How should the synchronized mechanism work? When entering a synchronized method, only threads that could interfere are automatically suspended... This solution is very complex. It is not not easy to predict which threads will do what in the future. It is therefore not easy to find out which threads should be suspended. Programação Concorrente e Distribuída 10

How should the synchronized mechanism work? Define specific methods and other code sections that contain critical sections. Before entering a critical section, a thread checks if another thread is already executing a related critical section and waits if that is the case. Otherwise, the thread reserves the object and start executing the critical section. This is the actual solution that Java uses. It is efficient because it only causes threads that would otherwise interfere to be suspended. Programação Concorrente e Distribuída 11

Mutex: mutual exclusion A basic locking mechanism in multithreaded programming A mutex-lock has to basic operations: Lock Unlock Only one thread can own the lock at any time. If a thread tries to lock a mutex that is already locked by another thread, it will have to wait until the lock becomes free (unlocked). If multiple thread are waiting for a mutex to become unlocked, only one thread will be able to lock the mutex when it happens. Programação Concorrente e Distribuída 12

Intrinsic locks and synchronization In Java every object has an intrinsic lock By convention a thread that needs exclusive and consistent access to an object's data must acquire the object's intrinsic lock before accessing its data. It should release the intrinsic lock when it is done. The thread owns the intrinsic lock between the act of acquiring and releasing. It is the programmer's responsibility to define the methods or blocks should be subject to the owning of the lock. This is done in Java using the synchronized keyword. Programação Concorrente e Distribuída 13

Synchronized methods class Pilha { private int pos = 0; private int pilha[]= new int[100]; public synchronized void push(int i) { pos ++; pilha[pos ]=i; Request Lock Free Lock public synchronized int pop() { if (pos >0){ return(pilha[pos --]); else return 0; public synchronized int peak() { return pilha[pos ]; Programação Concorrente Programação e Concorrente Distribuída e Distribuída 2007/2008 14

Synchronized Blocks In synchronized blocks we must specify the object from which the lock will be used. Example: public void addname(string name) { synchronized(this) { lastname = name; namecount++; namelist.add(name); public void addemploye(string name) { synchronized(boss) { namecount++; namelist.add(name); Request this lock Free this lock Request boss lock Free boss lock Programação Concorrente e Distribuída 2007/2008 15

Synchronized Blocks public class MsLunch { private long c1 = 0; private long c2 = 0; private Object lock1 = new Object(); private Object lock2 = new Object(); public void inc1() { synchronized(lock1) { c1++; public void inc2() { synchronized(lock2) { c2++; Programação Concorrente e Distribuída 16

To read... Object's API Recommended bibliography. Proposed exercises. Programação Concorrente e Distribuída 17

Bibliography JAVA tutorial: http://download.oracle.com/javase/tutorial/essential/concurrency/sync.html JAVA Threads, O Reilly, chapter 3 Multithreaded Programming, Lewis & Berg, chapters 5-6 Programação Concorrente e Distribuída 18

Summary Threads Synchronization: Interference and memory consistency errors. Atomic instructions and actions Mutex locks Synchronized methods Synchronized blocks Programação Concorrente e Distribuída 19