Docsity

Concurrency Overview: Language Support, Shared Memory, Messaging, and Sync - Prof. Elsa Gu, Study notes of Computer Science

Partial preview of the text

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

Concurrency OverviewLanguage Support Shared Memory and Message Passing Synchronization

CS421 Lecture 22: Concurrency^1

Mark Hills [email protected]

University of Illinois at Urbana-Champaign

August 3, 2006

(^1) Based on slides by Mattox Beckman, as updated by Vikram Adve, Gul Agha, and Elsa Gunter

Concurrency OverviewLanguage 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 2 ... 3 begin 4 ... 5 end T; 6 ... 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 { 2 ... 3 CClass(...) { 4 // constructor 5 } 6 ... 7 public void run() { 8 // code that runs in the thread 9 } 10 } 11 ... 12 CClass myclass = new CClass(...); 13 myclass.start(); 14 ... 15 myclass.join();

Concurrency OverviewLanguage 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 OverviewLanguage 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 OverviewLanguage 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 OverviewLanguage 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 OverviewLanguage 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 OverviewLanguage 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