More on Thread Safety CSE451 Andrew Whitaker. Review: Thread Hazards Safety hazards  “Program does the wrong thing” Liveness hazards  “Program never.

Slides:



Advertisements
Similar presentations
50.003: Elements of Software Construction Week 6 Thread Safety and Synchronization.
Advertisements

CS492B Analysis of Concurrent Programs Lock Basics Jaehyuk Huh Computer Science, KAIST.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 24 th, 2010 The University of Georgia.
Concurrency 101 Shared state. Part 1: General Concepts 2.
An Introduction to Programming with Threads. Resources Birrell - "An Introduction to Programming with Threads" Silberschatz et al., 7 th ed, Chapter 4.
©SoftMoore ConsultingSlide 1 Appendix D: Java Threads "The real payoff of concurrent execution arises not from the fact that applications can be speeded.
Designing a thread-safe class  Store all states in public static fields  Verifying thread safety is hard  Modifications to the program hard  Design.
CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections Dan Grossman Spring 2010.
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.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
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.
1 Where did the synchronized methods go? Yes, there still exists the synchronized keyword. public synchronized void foo() {} public void foo(){ aLock.lock();
Principle 1 If more than one thread accesses a given state variable and one of them might write to it then All accesses to the variable must be correctly.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
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.
1 MATERI PENDUKUNG SINKRONISASI Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
Week 9 Building blocks.
Pthread (continue) General pthread program structure –Encapsulate parallel parts (can be almost the whole program) in functions. –Use function arguments.
50.003: Elements of Software Construction Week 8 Composing Thread-safe Objects.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
Sharing Objects  Synchronization  Atomicity  Specifying critical sections  Memory visibility  One thread’s modification seen by the other  Visibility.
COMPSCI 230 S2C 2015 Software Design and Construction Synchronization (cont.) Lecture 4 of Theme C.
Threads Tutorial #7 CPSC 261. A thread is a virtual processor Each thread is provided the illusion that it owns a core – Copy of the registers – It is.
Multiprocessor Cache Consistency (or, what does volatile mean?) Andrew Whitaker CSE451.
Software and Threading Geza Kovacs Maslab Abstract Design: State Machines By using state machine diagrams, you can find flaws in your behavior without.
System Programming Practical Session 4: Concurrency / Safety.
HXY Debugging Made by Contents 目录 History of Java MT Sequential & Parallel Different types of bugs Debugging skills.
Project 2 Overview (Threads in Practice) CSE451 Andrew Whitaker.
System Programming Practical Session 4: Concurrency / Safety.
CSC Multiprocessor Programming, Spring, 2012 Outline for Chapter 4 – Composing Objects – thread-safe object-oriented composition, Dr. Dale E. Parson,
Component-Based Software Engineering Understanding Thread Safety Paul Krause.
Agenda  Quick Review  Finish Introduction  Java Threads.
Concurrent Programming Acknowledgements: Some slides adapted from David Evans, U. Virginia.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
Multiplication Find the missing value x __ = 32.
pThread synchronization
Monitor Pattern Read only Collection views Synchronized Collections views Concurrent HashMap The Vehicle Tracker Example.
Sun Proprietary/Confidential: Internal Use Only 1 Multi-Threading Primer Byron Nevins December 10, 2007.
December 1, 2006©2006 Craig Zilles1 Threads & Atomic Operations in Hardware  Previously, we introduced multi-core parallelism & cache coherence —Today.
Deadlock CSE451 Andrew Whitaker. Review: What Can Go Wrong With Threads? Safety hazards  “Program does the wrong thing” Liveness hazards  “Program never.
Kernel Synchronization David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
PARALLEL PROGRAM CHALLENGES
Atomic Operations in Hardware
Atomic Operations in Hardware
CSE 332: Locks and Deadlocks
Thread synchronization
Deadlock, Illustrated.
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
More on Thread Safety CSE451 Andrew Whitaker.
Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619.
Java Concurrency 17-Jan-19.
EE 4xx: Computer Architecture and Performance Programming
Userspace Synchronization
Java Concurrency.
Java Concurrency.
Lecture 8 Thread Safety.
CSE 451 Section 1/27/2000.
Java Concurrency 29-May-19.
Problems with Locks Andrew Whitaker CSE451.
CSE 332: Concurrency and Locks
Process/Thread Synchronization (Part 2)
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
Threads and concurrency / Safety
Presentation transcript:

More on Thread Safety CSE451 Andrew Whitaker

Review: Thread Hazards Safety hazards  “Program does the wrong thing” Liveness hazards  “Program never does the right thing” e.g., deadlock Performance hazards  Program is too slow due to excessive synchronization

Safety Rule #1 All shared, mutable state must be properly synchronized  Usually with a synchronized block or method

Why Not Make Every Method Synchronized? Synchronization has a performance cost  Each lock access is a cache miss  Synchronization limits parallelism Rampant synchronization leads to deadlock Available parallelism Amount of synchronization

Safety Rule #1, Revised All shared, mutable state must be properly synchronized  Usually with a synchronized block or method Corollaries:  Do not synchronize for non-shared state Accessed by a single thread  Do not synchronize on immutable state

public class Foo { public int addSix(int arg) { synchronized (this) { arg += 6; } return arg; } This is unnecessary! No need to synchronize on non-shared state.

// Thread-safe public class Integer { private final int x; public Integer(int arg) { this.x = arg; } public synchronized int get() { return x; } Immutable state does not require synchronization!

Example from Project #1 Do the execcounts counters require synchronized access? Technically, yes:  The counters are shared across multiple processes/threads  The counters are mutable Solution: atomic variables  See Linux /include/asm-i386/atomic.h You do not need to do this for project #1

Another Example: AspectRatio

Rule #2 Compound actions must be protected by a single lock  Often, we talk about protecting an invariant with a single lock e.g., aspect ratio remains fixed

Linked List Example