INF 212 ANALYSIS OF PROG. LANGS CONCURRENCY. Instructors: Crista Lopes Copyright Instructors.

Similar documents
Le L c e t c ur u e e 7 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Multithreading

Unit 4. Thread class & Runnable Interface. Inter Thread Communication

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

04-Java Multithreading

Unit - IV Multi-Threading

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

CMSC 330: Organization of Programming Languages

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

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

JAVA. Lab 12 & 13: Multithreading

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

7. MULTITHREDED PROGRAMMING

Processes and Threads

Dealing with Issues for Interprocess Communication

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

THREADS & CONCURRENCY

Intel Threading Tools

CS 475. Process = Address space + one thread of control Concurrent program = multiple threads of control

Threads Chate Patanothai

Animation Part 2: MoveableShape interface & Multithreading

Synchronization

CS 351 Design of Large Programs Threads and Concurrency

Multithreaded Programming

Systems Programming & Scripting

Threads. Definitions. Process Creation. Process. Thread Example. Thread. From Volume II

Amity School of Engineering

CMSC 132: Object-Oriented Programming II. Threads in Java

CS510 Operating System Foundations. Jonathan Walpole

Review: Easy Piece 1

CMSC 132: Object-Oriented Programming II

Chair of Software Engineering. Java and C# in Depth. Prof. Dr. Bertrand Meyer. Exercise Session 8. Nadia Polikarpova

THREADS AND CONCURRENCY

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

9/23/2014. Concurrent Programming. Book. Process. Exchange of data between threads/processes. Between Process. Thread.

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

Synchronization synchronization.

Processes and Threads. Industrial Programming. Processes and Threads (cont'd) Processes and Threads (cont'd)

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

Parallel Programming Languages COMP360

Multithreading using Java. Dr. Ferdin Joe John Joseph

CS193k, Stanford Handout #8. Threads 3

Concurrency, Thread. Dongkun Shin, SKKU

Multithreaded Programming

Exercise Session Week 8

COMP 346 WINTER Tutorial 2 SHARED DATA MANIPULATION AND SYNCHRONIZATION

CS510 Operating System Foundations. Jonathan Walpole

i219 Software Design Methodology 11. Software model checking Kazuhiro Ogata (JAIST) Outline of lecture

Threads. Threads (continued)

COURSE 11 PROGRAMMING III OOP. JAVA LANGUAGE

Threads and Too Much Milk! CS439: Principles of Computer Systems February 6, 2019

Overview. Processes vs. Threads. Computation Abstractions. CMSC 433, Fall Michael Hicks 1

Introducing Shared-Memory Concurrency

CS 3305 Intro to Threads. Lecture 6

Threads. Still Chapter 2 (Based on Silberchatz s text and Nachos Roadmap.) 3/9/2003 B.Ramamurthy 1

Exercise Session Week 8

Multithreaded Programming

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17

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

Threads, Concurrency, and Parallelism

AC OB S. Multi-threaded FW framework (OS) for embedded ARM systems Torsten Jaekel, June 2014

Programming in Parallel COMP755

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Threads and Too Much Milk! CS439: Principles of Computer Systems January 31, 2018

Threading and Synchronization. Fahd Albinali

4. Concurrency via Threads

Module - 4 Multi-Threaded Programming

COMP 213. Advanced Object-oriented Programming. Lecture 23. Shared Variables and Synchronization

Motivation of Threads. Preview. Motivation of Threads. Motivation of Threads. Motivation of Threads. Motivation of Threads 9/12/2018.

Deadlock. Only one process can use the resource at a time but once it s done it can give it back for use by another process.

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

Concurrent Computing CSCI 201 Principles of Software Development

UNIT IV MULTITHREADING AND GENERIC PROGRAMMING

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

Concurrency: a crash course

CS333 Intro to Operating Systems. Jonathan Walpole

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

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

Threads & Concurrency

Threads & Concurrency

Threads and Java Memory Model

Agenda. Threads. Single and Multi-threaded Processes. What is Thread. CSCI 444/544 Operating Systems Fall 2008

THREADS & CONCURRENCY

CSCE 313: Intro to Computer Systems

CS 153 Design of Operating Systems Winter 2016

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Lecture 7 Java SE Multithreading

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

Distributed systems. Programming with threads

Java Threads. Introduction to Java Threads

Writing Parallel Programs COMP360

EECS 482 Introduction to Operating Systems

CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives

Advanced Programming Methods. Lecture 6 - Concurrency in Java (1)

Lecture 4. Threads vs. Processes. fork() Threads. Pthreads. Threads in C. Thread Programming January 21, 2005

Note: Each loop has 5 iterations in the ThreeLoopTest program.

Threads Questions Important Questions

package p1; public class Derivation extends Protection { public Derivation() { System.out.println("Derived class constructor");

CS 5523: Operating Systems

Process Characteristics. Threads Chapter 4. Process Characteristics. Multithreading vs. Single threading

Transcription:

INF 212 ANALYSIS OF PROG. LANGS CONCURRENCY Instructors: Crista Lopes Copyright Instructors.

Basics

Concurrent Programming More than one thing at a time Examples: Network server handling hundreds of clients Data processing spread across multiple CPUs App receiving input from several peripherals

Concurrency 1 CPU Thing 1 run run run switch Scheduler Thing 2 run run run

Concurrency Many CPUs When there are more things than CPUs, CPUs are shared like previous slide Thing 1 run CPU 1 Thing 2 run CPU 2

Processing vs. IO Tasks alternate between processing and IO Thing 1 run io io io io IO must wait for data Runtime system will resume task when data becomes available

CPU-bound tasks When it spends most of the time processing CPU is busy Thing 1 run Examples: Math Image processing

IO-bound tasks When it spends most of the time doing IO CPU is idle Thing 1 run Examples: User input Networking Files

Shared memory Simultaneous access to memory space Process Thing 1 run CPU 1 Memory Thing 2 run CPU 2

Separate processes, shared machine No shared memory Inter-Process Communication (IPC) Process Thing 1 run Memory CPU 1 Process IPC Memory Thing 2 run CPU 2

Different machines, distributed computing Network Sockets Process Thing 1 run Memory Machine 1 Process Messages Memory Thing 2 run Machine2

Shared Memory Threads

Simplified Machine Model slide 13 Registers Code Data PROCESS Stack Program counter Environment pointer Heap

Threads heap heap

Threads: benefits Responsiveness Resource sharing [Performance in multi-core machines]

Threads: pitfalls Race conditions Heisenbugs Easy to end up with a colossal code mess Heavy startup time for new threads [Thread pools to the rescue]

Two Types of Threads 17 User-level threads libraries: - POSIX Pthreads - Java Threads -.NET Threads - Python Threads -... This course Kernel threads

Thread API Examples

OOP Threads (Java/C#/PHP/Python) v.1 instantiate Thread class ThreadedObject implements Runnable { Thread t; ThreadedObject() { // Create a thread t = new Thread(this, "Demo Thread"); System.out.println("Child thread: " + t); t.start(); // Start the thread Thread runtime calls run // This is the entry point for the ThreadedObject thread. public void run() { for (int i = 5; i > 0; i--) { System.out.println("Child Thread: " + i); // Let the thread sleep for a while. Thread.sleep(50); System.out.println("Exiting child thread.");

OOP Threads (Java/C#/PHP/Python) v.1 instantiate Thread public class ThreadDemo { public static void main(string args[]) { new ThreadedObject(); // create a new thread for (int i = 5; i > 0; i--) { System.out.println("Main Thread: " + i); Thread.sleep(100); System.out.println("Main thread exiting.");

OOP Threads (Java/C#/PHP/Python) (v2 extend Thread) class ThreadedObject extends Thread { ThreadedObject() { super("demo Thread"); System.out.println("Child thread: " + this); start(); // Start the thread Thread runtime calls run // This is the entry point for the thread. public void run() { for (int i = 5; i > 0; i--) { System.out.println("Child Thread: " + i); // Let the thread sleep for a while. Thread.sleep(50); System.out.println("Exiting child thread.");

OOP Threads (Java/C#/PHP/Python) (v2 extend Thread) public class ThreadDemo { public static void main(string args[]) { new ThreadedObject(); // create a new thread for (int i = 5; i > 0; i--) { System.out.println("Main Thread: " + i); Thread.sleep(100); System.out.println("Main thread exiting.");

Functional Threads (C/C++/Perl/Racket) #lang racket Function to run (thread (lambda () (for ([i 10]) (sleep 2) (printf "thread 1\n")))) (thread (lambda () (for ([i 20]) (sleep 1) (printf "thread 2\n"))))

Functional Threads (C/C++/Perl/Racket) #include <string> #include <iostream> #include <thread> using namespace std; //The function we want to make the thread run. void task1(string msg) { cout << "task1 says: " << msg; Function to run int main() { // Constructs the new thread and runs it. Does not block thread t1(task1, "Hello"); //Makes the main thread wait for the new thread to finish t1.join();

Common operations on threads Create Start Sleep Suspend Resume Yield Stop (you ll find most of these in most thread APIs)

Concurrency Control

Threads shared resources heap heap

Shared data problems class ThreadedObject extends Thread { Counter c; ThreadedObject _c) { super("demo Thread"); System.out.println("Child thread: " + this); c = _c; start(); // Start the thread public void run() { for (int i = 500; i > 0; i--) { System.out.println("Child Thread: " + i); if (i % 2 == 0) c.increment(); else c.decrement(); Thread.sleep(50); System.out.println("Exiting child thread.");

Shared data problems class Counter { private int c = 0; public void increment() { c = c + 1; public void decrement() { c = c 1;

Shared data problems public class ThreadDemo { public static void main(string args[]) { Counter c = new Counter(); ThreadedObject[] ao = new ThreadedObject[5]; // Create 5 threads for (int i = 5; i > 0; i--) ao[i] = new ThreadedObject(c); // create a new thread // Wait for the threads to finish for (ThreadedObject a : ao) a.join(); System.out.println("Main thread exiting.");

Shared data problems Counter c increment decrement Threaded Object Threaded Object Threaded Object Threaded Object Threaded Object

Things that can go wrong class Counter { private int c = 0; T1 T2 public void increment() { c = c + 1; public void decrement() { c = c 1; Race condition One action lost: T1 enters the method T1 reads value of c (e.g. 3) T1 is interrupted by scheduler T2 enters the method T2 reads value of c (3) T2 adds 1 to c (4) T2 assigns c to the new value (4) T2 returns T1 is resumed T1 adds 1 to [old] value of c (4) T1 assigns c to that value (4)

Things that can go wrong class Counter { private int c = 0; T1 public void increment() { c = c + 1; public void decrement() { c = c 1; T2 One action lost: T1 enters increment T2 enters decrement T1 reads value of c (e.g. 3) T2 reads value of c (3) T1 adds 1 to its value of c (4) T2 subtracts 1 to its value of c (2) T1 assigns new value to c (4) T2 assigns c (2) Race condition

Race conditions Corruption of shared data due to thread scheduling Very hard to deal with Non-deterministic Hard to reproduce Program may be ok for long time until it hits a race condition

Concurrency control Primitives for controlling the execution of concurrent threads over the same code

Concurrency control (Java) class Counter { private int c = 0; public synchronized void increment() { c = c + 1; public synchronized void decrement() { c = c 1; Only one thread at a time can enter the synchronized methods

Concurrency control (others) class Counter { private int c = 0; Object o = new Object(); public void increment() { lock (o) { c = c + 1; public void decrement() { lock (o) { c = c 1; Only one thread at a time can enter the locked blocks

Concurrency control (others ) class Counter { private int c = 0; Lock o = new Lock(); public void increment() { o.acquire(); c = c + 1; o.release(); public void decrement() { o.acquire(); c = c 1; o.release(); Only one thread at a time can enter the locked blocks

Locks They seem easy They are very hard to manage

Lock management Acquired locks must always be released Including when exceptions occur A lock that is not released will prevent any other thread from entering the block forever

Over-locking Deadlocks class Counter { private int c = 0; Object o1 = new Object(); Object o2 = new Object(); public void increment() { lock (o1) { lock (o2) { c = c + 1; o1, then o2 public void decrement() { lock (o2) { lock (o1) { c = c 1; o2, then o1 AVOID!

Threads Summary Nice, but... Synchronization primitives are hard to get right Deadlocks Race conditions Many corner cases Bugs hard to reproduce Potentially bad performance (locking is heavy) Many reasons to avoid threads!... or at least to use them under strict constraints