Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of...

22
Concurrent Programming Yannis Smaragdakis University of Massachusetts, Amherst

Transcript of Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of...

Page 1: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

Concurrent Programming

Yannis SmaragdakisUniversity of Massachusetts, Amherst

Page 2: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

Obligatory Moore’s Law Slideg yNumber of transistors doubles every 2 years

Surface of Sun

Rocket Nozzle

Surface of Sun

# Transistors/

Chip

Pow

er

Nuclear Reactor

Rocket Nozzle

C p Density (

Hot Plate

W/cm

2)

2

Page 3: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

Y H H d f M lti CYou Have Heard of Multi-Cores

You have probably also heard that parallelYou have probably also heard that parallel programming is hardM t d ll l iMany ways to do parallel programming

message passingltith dimultithreading

…S it d f diff t ki d f ll liSuited for different kinds of parallelism

e.g., shared memory vs. distributed memory

Yannis SmaragdakisUniversity of Massachusetts, Amherst

3

Page 4: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

This Talk: Only AboutM ltith diMultithreading

Most explicit parallel programming modelMost explicit parallel programming model, unlikely to be completely superseded for general purpose shared memory parallelismgeneral purpose shared memory parallelism

Yannis SmaragdakisUniversity of Massachusetts, Amherst

4

Page 5: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

Quick Review of M ltith diMultithreading

A thread is a single sequential flow of controlA thread is a single sequential flow of controla process can have many threads and a single address spaceaddress spacethreads share memory and, hence, need to cooperate to produce correct resultsp pshared memory: heap, global variablesthread-private memory: stack, other only if p y yexplicitly designated

Yannis SmaragdakisUniversity of Massachusetts, Amherst

5

Page 6: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

Programming Models for M ltith diMultithreading

Most thread programming nowadays isMost thread programming nowadays is “monitor style” programming

introduced in 70s subsequently dominated allintroduced in 70s, subsequently dominated all alternatives as a general-purpose facility

alternatives: semaphores, low-level atomic operations, p , p ,etc.

universal mechanism: can do anything

Yannis SmaragdakisUniversity of Massachusetts, Amherst

6

Page 7: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

O tliOutline

1 Monitor style programming: traditional1. Monitor style programming: traditional multithreading

if you already know mutexes bear with me for 30if you already know mutexes, bear with me for 30 mins and you’ll see new stuff

2. Complexities of the programming modelC p p g g3. Transactional memory: an emerging MT

programming modelprogramming modelComplexitiesImplementation

Yannis SmaragdakisUniversity of Massachusetts, Amherst

7

Page 8: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

MONITOR STYLEMONITOR STYLE PROGRAMMING

Yannis SmaragdakisUniversity of Massachusetts, Amherst

8

Page 9: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

M it St l P iMonitor Style ProgrammingTwo components:Two components:

locks/mutexes (lock)condition variables (wait signal broadcast)condition variables (wait, signal, broadcast)

Mapping of abstract to concrete:Java: every object is a mutex and a condition varJava: every object is a mutex and a condition var

lock -> synchronized

wait -> Object.wait

signal -> Object.notify

broadcast -> Object.notifyAllYannis Smaragdakis

University of Massachusetts, Amherst9

Page 10: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

Oth I tiOther Incarnations

PThreads (C C++ etc ): can declarePThreads (C, C++, etc.): can declare explicit mutex and condition var objects

th d t t th d d tpthread_mutex_t, pthread_cond_toperations:

lock -> pthread_mutex_lock ... pthread_mutex_unlock

wait, signal, broadcast -> pthread_cond_{wait,signal,broadcast}

Yannis SmaragdakisUniversity of Massachusetts, Amherst

10

Page 11: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

F O Di iFor Our Discussion

Default Java distorts and handicaps theDefault Java distorts and handicaps the model a bit. We will use an imaginary language with the full power (also supportedlanguage with the full power (also supported in Java through library classes)

Java but with condition variables declaredJava but with condition variables declared explicitly as objects of type Cond

Thread creation is an uninteresting detailThread creation is an uninteresting detailin Java threads are instances of class Thread, they begin execution when the start method is y gcalled

Yannis SmaragdakisUniversity of Massachusetts, Amherst

11

Page 12: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

M tMutexes

Mutexes are used to control access to sharedMutexes are used to control access to shared data

only one thread can execute inside aonly one thread can execute inside a synchronized clauseother threads who try to enter synchronized codeother threads who try to enter synchronized code, are blocked until the mutex is unlocked

As we said, in Java, every object is a (or “has , , y j (an associated”) mutex!

Yannis SmaragdakisUniversity of Massachusetts, Amherst

12

Page 13: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

M tMutexesclass List {class List {public synchronized int insert(int i){ [BODY] }

}}

same asclass List {class List {public int insert(int i){ synchronized(this) [BODY] }

}

Yannis SmaragdakisUniversity of Massachusetts, Amherst

13

Page 14: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

M t P t t i l E lMutexes Prototypical Exampleclass Account {int balance = 0;public synchronized int withdraw(int amt) {if (balance >= amt) {b l tbalance -= amt;return amt;

} else return 0;

}}public synchronized void deposit(int i) {balance += i;}}

}

what errors are prevented?we’ll return to this example

Yannis SmaragdakisUniversity of Massachusetts, Amherst

14

Page 15: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

U i M t D dl kUsing Mutexes: Deadlocks

Example deadlock:Example deadlock:A locks M1, B locks M2, A blocks on M2, B blocks on M1on M1

Techniques for avoiding deadlocks:Fine grained locking (but then greater risk of races)Fine grained locking (but then greater risk of races)Two-phase locking: acquire up front all the locks you’ll need, release all if you fail to acquire any oneyou ll need, release all if you fail to acquire any oneOrder locks and acquire them in order (e.g., all threads first acquire M1, then M2)

Yannis SmaragdakisUniversity of Massachusetts, Amherst

15

Page 16: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

C diti V i blCondition VariablesUsed to wait for specific events (especially forUsed to wait for specific events (especially for long/indefinite waits)

free memory is getting low wake up the garbagefree memory is getting low, wake up the garbage collector thread10,000 clock ticks have elapsed, update that p pwindownew data arrived in the I/O port, process it

Each condition variable is associated with a single mutex

In Java, every object is cond. var (ugly!)Yannis Smaragdakis

University of Massachusetts, Amherst16

Page 17: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

Condition Variables in Our LLanguage

Cond type with methodsCond type with methodswait(m): atomically unlocks the mutex (as many times as needed) and blocks the threadtimes as needed) and blocks the threadnotify: awakes some blocked thread

the thread is awoken inside waitthe thread is awoken inside waittries to lock the mutex (maybe many times)when it (finally) succeeds it returns from the waitwhen it (finally) succeeds, it returns from the wait

notifyAll: like notify but for all blocked threadst eads

Yannis SmaragdakisUniversity of Massachusetts, Amherst

17

Page 18: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

C diti V E lCondition Var Exampleclass Buffer {{Port port;Cond c = new Cond();public synchronized void consume() {public synchronized void consume() {while (port.empty())c.wait(this);

process data(port.first data());process_data(port.first_data());}

public synchronized void produce() {public synchronized void produce() {port.add_data();c.notifyAll();

}}}

Yannis SmaragdakisUniversity of Massachusetts, Amherst

18

Page 19: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

Using Condition Variablesgclass Buffer {Port port;Cond c = new Cond();public synchronized void consume() {while (port.empty())c.wait(this);

process_data(port.first_data());}

bli h i d id d () {public synchronized void produce() {port.add_data();c.notify();

}}}

Why not “if”? When can we use notifyAll? Could y ywe replace condition variables with messaging?

Yannis SmaragdakisUniversity of Massachusets, Amherst

19

Page 20: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

Monitor Style Programming G l P ttGeneral Pattern

Armed with mutexes and condition variables, one ,can implement any kind of critical section

CS.enter(); [controlled code] CS.exit();

l CS {class CS {[shared data, including c]public synchronized void enter() {while (![condition]) c wait(this); while (![condition]) c.wait(this); [change shared data to reflect in_CS][notify as needed]

}}public synchronized void exit() {[change shared data to reflect out_of_CS][notify as needed]y

}}

Yannis SmaragdakisUniversity of Massachusetts, Amherst

20

Page 21: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

Simplest Example: Implement U t t d M tan Unstructured Mutex

class Mutex {class Mutex {

bli h i d id i () {public synchronized void acquire() {

}public synchronized void release() {

}}}

Yannis SmaragdakisUniversity of Massachusetts, Amherst

21

Page 22: Concurrent Programming - UO Computer and Information ...€¦ · YH HdfMltiYou Have Heard of Multi-Cores zYou have probably also heard that parallelYou have probably also heard that

Simplest Example: Implement U t t d M tan Unstructured Mutex

class Mutex {class Mutex {boolean locked = false;Cond c = new Cond();bli h i d id i () {public synchronized void acquire() {while (locked) c.wait(this); locked = true;;

}public synchronized void release() {

locked = false;locked = false;c.notify();

}}}

Yannis SmaragdakisUniversity of Massachusetts, Amherst

22