Docsity

Concurrency: Language Support, Shared Memory, and Synchronization - Prof. Elsa Gunter, Study notes of Computer Science

Toggle sidebar
bg1
Outline
Concurrency Overview
Language Support
Shared Memory and Message Passing
Synchronization
CS421 Lecture 21: Concurrency1
Mark Hills
University of Illinois at Urbana-Champaign
July 28, 2008
1Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul
Agha, and Elsa Gunter
Mark Hills CS421 Lecture 21: Concurrency 1 / 23
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Concurrency: Language Support, Shared Memory, and Synchronization - Prof. Elsa Gunter and more Study notes Computer Science in PDF only on Docsity!

Concurrency Overview Language Support Shared Memory and Message Passing Synchronization

CS421 Lecture 21: Concurrency

Mark Hills

[email protected]

University of Illinois at Urbana-Champaign

July 28, 2008

Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul

Agha, and Elsa Gunter

Concurrency Overview Language Support Shared Memory and Message Passing Synchronization

Concurrency Overview

Language Support

Shared Memory and Message Passing

Synchronization

Concurrency Overview Language Support Shared Memory and Message Passing Synchronization

Options in Concurrency

In a language with concurrency support, we have many design

options. These are the big three, but they lead to others...

◮ Library or Language: Is support for concurrency baked directly

into the language, or is it provided by system libraries?

◮ Shared Memory or Message Passing: Do threads communicate

by reading and writing shared areas of memory, or by passing

messages back and forth?

◮ Synchronization: How do threads make sure that operations

occur in the desired order?

Concurrency Overview Language Support Shared Memory and Message Passing Synchronization

Using Libraries

Libraries are still the most common way to support concurrency.

◮ For shared memory systems, pthreads is the most common

option

◮ Vendors often provide their own thread packages as well

(Microsoft has similar support for Windows, for instance)

◮ For message passing, PVM and MPI are both very popular,

with (my opinion) MPI probably moreso now

Concurrency Overview Language Support Shared Memory and Message Passing Synchronization

Co-begin

A co-begin construct allows a standard block to be executed

either sequentially or in parallel, based on the chosen keywords.

From Algol 68:

1 # from Algol 68

2 par begin

3 p(a, b, c),

4 begin

5 d := q(e, f);

6 r(d, g, h)

7 end,

8 s(i, j)

9 end

The comma-separated statements execute in parallel, but the

internal begin is sequentialized.

Concurrency Overview Language Support Shared Memory and Message Passing Synchronization

Parallel Loops

Parallel loops are available in some languages to allow each

iteration to be executed in parallel. Some optimizing compilers also

transform loops automatically into forms that can be executed in

parallel.

1 # from the SR language

2 co (i := 5 to 10) ->

3 p(a, b, i) # six instances of p

4 oc

Concurrency Overview Language Support Shared Memory and Message Passing Synchronization

Fork/Join

Instead of threads being worked in to the control flow, they can

also be explicitly defined and created. In Ada:

1 task type T is

3 begin

5 end T;

7 pt : access T := new T;

Concurrency Overview Language Support Shared Memory and Message Passing Synchronization

Fork/Join in Java

1 class CClass extends Thread {

3 CClass(...) {

4 // constructor

7 public void run() {

8 // code that runs in the thread

12 CClass myclass = new CClass(...);

13 myclass.start();

15 myclass.join();

Concurrency Overview Language Support Shared Memory and Message Passing Synchronization

Message Passing

Message passing systems all threads to communicate by sending

messages to one another. Messages generally come in two forms:

◮ Synchronous Messages: One thread sends a message to

another and waits for the message to be received; a variant

(RPC) actually waits for a reply as well

◮ Asynchronous Messages: One thread sends a message to

another and then continues computation

Synchronous is probably more common, and maybe (but not

always) makes more sense for threads communicating within a

process or on the same machine. Asynchronous makes more sense

for many (but not all) distributed computations.

Concurrency Overview Language Support Shared Memory and Message Passing Synchronization

Synchronous Message Passing

Many languages use a synchronous message passing model –

Concurrent ML, plus by default with most RPC and RMI

mechanisms.

◮ Advantage: allows direct synchronization on events between

threads

◮ Advantage: provides a sense of “shared state”; the threads

know something about where they each are in a computation

◮ Disadvantage: does not handle network problems, unreliable

or congested threads well

Concurrency Overview Language Support Shared Memory and Message Passing Synchronization

Asynchronous Message Passing

Asynchronous message passing is used in some languages and

language extensions, and is fundamental to the Actor model of

computation.

◮ Advantage: handles variable network and congestion

problems well

◮ Disadvantage: often requires some special infrastructure

(message buffers, etc) to work correctly

◮ Disadvantage: message receipt can be more involved (think

out of order receipt) and doesn’t convey as much

“information” as synchronous – need special data structures

(vector clocks, etc) to add this information

Concurrency Overview Language Support Shared Memory and Message Passing Synchronization

Synchronization

Synchronization is mainly a concern in shared-memory

applications. Message passing provides implicit synchronization on

message sends and receives. Several common options are:

◮ locks

◮ barriers

◮ semaphores

◮ monitors

Concurrency Overview Language Support Shared Memory and Message Passing Synchronization

Barriers

Barriers are often used in numerical computations.

◮ Provide for lock-step computation

◮ All threads do some work, and then signal when they have

reached the barrier

◮ When all threads have reached the barrier, all threads are

released again to continue

Concurrency Overview Language Support Shared Memory and Message Passing Synchronization

Semaphores

Semaphores are similar to locks, but can be used in more complex

ways.

◮ A binary semaphore is basically a lock

◮ Semaphores can count beyond 1, though – allowing use to

synchronize when counts are important (bounded buffer

problem, etc)

◮ P operation lowers count by one, waits until count is

non-negative

◮ V operation raises count by one