Concurrent Programming

Source: MIT 6.005 — Software Construction

http://web.mit.edu/6.005/www/fa14/classes/17-concurrency/

Concurrency

Concurrency means multiple computations are happening at the same time. Concurrency is everywhere in modern programming, whether we like it or not:

  • Multiple computers in a network

  • Multiple applications running on one computer

  • Multiple processors in a computer (today, often multiple processor cores on a single chip)

In fact, concurrency is essential in modern programming:

  • Web sites must handle multiple simultaneous users.

  • Mobile apps need to do some of their processing on servers (“in the cloud”).

  • Graphical user interfaces almost always require background work that does not interrupt the user. For example, Eclipse compiles your Java code while you’re still editing it.

Two Models for Concurrent Programming

There are two common models for concurrent programming:shared memory_and_message passing.

Shared memory.In the shared memory model of concurrency, concurrent modules interact by reading and writing shared objects in memory.

Other examples of the shared-memory model:

  • A and B might be two processors (or processor cores) in the same computer, sharing the same physical memory.

  • A and B might be two programs running on the same computer, sharing a common filesystem with files they can read and write.

  • A and B might be two threads in the same Java program (we’ll explain what a thread is below), sharing the same Java objects.

Message passing.In the message-passing model, concurrent modules interact by sending messages to each other through a communication channel. Modules send off messages, and incoming messages to each module are queued up for handling. Examples include:

  • A and B might be two computers in a network, communicating by network connections.

  • A and B might be a web browser and a web server – A opens a connection to B, asks for a web page, and B sends the web page data back to A.

  • A and B might be an instant messaging client and server.

  • A and B might be two programs running on the same computer whose input and output have been connected by a pipe, likels | greptyped into a command prompt.

Last updated