Thread Synchronization

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Synchronization NOTE to instructors: it is helpful to walk through an example such as readers/writers locks for illustrating the use of condition variables.
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.
– 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.
CMSC 330: Organization of Programming Languages Threads Classic Concurrency Problems.
1 Race Conditions When threads share access to a common object/data, they can conflict with each other and mess up the consistency of the object/data.
Li Tak Sing (Lecture 3) COMPS311F 1. Flexible synchronization with locks Synchronized methods and statement blocks: Advantages: Relatively easy to use.
1 Condition Synchronization. 2 Synchronization Now that you have seen locks, is that all there is? No, but what is the “right” way to build a parallel.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  synchronized methods and statements  wait.
Locks (Java 1.5) Only one thread can hold a lock at once –Other threads that try to acquire it block (or become suspended) until lock becomes available.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
1 Semaphores and Monitors: High-level Synchronization Constructs.
CS 11 java track: lecture 7 This week: Web tutorial:
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.
© Andy Wellings, 2004 Roadmap  Introduction  Concurrent Programming  Communication and Synchronization  Completing the Java Model  Overview of the.
Language Support for Concurrency. 2 Common programming errors Process i P(S) CS P(S) Process j V(S) CS V(S) Process k P(S) CS.
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Multithreading.
Discussion Week 3 TA: Kyle Dewey. Overview Concurrency overview Synchronization primitives Semaphores Locks Conditions Project #1.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 Thread II Slides courtesy of Dr. Nilanjan Banerjee.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CSE 219 Computer Science III Multithreaded Issues.
Atomic Operations David Monismith cs550 Operating Systems.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
CSE 501N Fall ‘09 23: Advanced Multithreading: Synchronization and Thread-Safety December 1, 2009 Nick Leidenfrost.
Multi-Threaded Programming Design CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Internet Software Development Controlling Threads Paul J Krause.
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.
1 G53SRP: Java Concurrency Control (1) - synchronisation Chris Greenhalgh School of Computer Science.
OPERATING SYSTEMS Frans Sanen.  Recap of threads in Java  Learn to think about synchronization problems in Java  Solve synchronization problems in.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Synchronizing Threads with Semaphores
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Dining Philosophers & Monitors Questions answered in this lecture: How to synchronize dining philosophers? What are monitors and condition variables? What.
CS 151: Object-Oriented Design November 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Producer/Consumer CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
pThread synchronization
Multithreading / Concurrency
CSE 120 Principles of Operating
Synchronization and Scheduling
Monitors.
Synchronization Lecture 23 – Fall 2017.
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Producer-Consumer Problem
Chapter 7: Synchronization Examples
Race conditions and Synchronization
Chapter 30 Condition Variables
Real Time Java : Synchronization
CSCI1600: Embedded and Real Time Software
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
CSCI1600: Embedded and Real Time Software
Monitors and Inter-Process Communication
EECE.4810/EECE.5730 Operating Systems
Software Engineering and Architecture
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

Thread Synchronization http://flic.kr/p/fMVpEu Thread Synchronization

So why would you want concurrency anyway? http://flic.kr/p/9ksxQa

Why is this sequential design lame? How might concurrency improve it? public class MySequentialServer { … public static void main(String[] args) { for (;;) { Connection conn = socket.accept(); service(conn); } } } Why is this sequential design lame? How might concurrency improve it? http://flic.kr/p/9ksxQa

So why would you want concurrency anyway? Performance. Increase responsiveness. Do more work in less time. Increase throughput. http://flic.kr/p/9ksxQa

Here’s one possible concurrent server design 1 Handler 1..* buf for (;;) { service(buf.get()); } 1 Listener Buffer put(c : Connection) get() : Connection buf data : Connection[] 1 for (;;) { buf.put(socket.accept()); } See any problems with it?

Active objects Shared passive object Error!

Initially, there is one connection c in the buffer H1 executes first Initially, there is one connection c in the buffer Error!

Error!

Error!

H1 removes the connection from the buffer Error!

H2 tries to pull from an empty buffer Error! H2 tries to pull from an empty buffer

So race conditions in the buffer are a problem with this design 1 Handler 1..* buf for (;;) { service(buf.get()); } 1 Listener Buffer put(c : Connection) get() : Connection buf data : Connection[] 1 for (;;) { buf.put(socket.accept()); }

How do we prevent race conditions? Answer: Synchronization! Java provides two basic mechanisms: Mutex locks For enforcing mutually exclusive access to shared data Condition variables For enabling threads to wait for application-specific conditions to become true http://flic.kr/p/aGcqRp

Mutex locks States: Operations: Operations are atomic unlocked locked by exactly 1 thread Operations: lock() (aka acquire) unlock() (aka release) Operations are atomic Threads that call lock() on a held lock must wait http://flic.kr/p/678N6L

Mutex locks class X { private final ReentrantLock mutex = new ReentrantLock(); // ... public void m() { mutex.lock(); try { // ... method body ... } finally { mutex.unlock() } http://flic.kr/p/678N6L

Lock not held (no waiters) Lock held by T1 (still no waiters)

T1 releases and T2 becomes holder T2 waits on lock T1 releases and T2 becomes holder

Can you fix our server example now? In Java, every object has an intrinsic lock (inherited from class Object) Java provides a synchronized keyword for doing implicit acquires/releases class X { public synchronized void m() { // ... method body ... } Can you fix our server example now?

Handling a full/empty buffer is still a problem What we’d like to happen is Listener to wait while the buf is full Handlers to wait while the buf is empty Condition variables to the rescue!

Condition variables Object for enabling waiting for some condition Always associated with a particular mutex Mutex must be held while invoking operations Operations await() (aka wait) signal() (aka notify) signalAll() (aka notifyAll; aka broadcast)

Condition variables class Buffer { final Lock lock = new ReentrantLock(); final Condition notFull = lock.newCondition(); // ... public void put(Object x) throws ... { lock.lock(); try { while (isFull()) { notFull.await(); } // ... do the put ... notEmpty.signal(); } finally { lock.unlock();

Objects also have intrinsic condition variables class Buffer { public synchronized void put(Object x) { while (isEmpty()) { wait(); } // ... do the put ... notify();

T1 holds the lock No waiters

Note: T1 still in the call to wait

What you’ve just seen is the Monitor Pattern Monitor object: An object whose methods are executed in mutual exclusion http://flic.kr/p/7ZkGEH