Concurrency

Back to UWaterloo

Summary

Introduction

Concurrency is advanced code control flow.

Dynamic Multi-Level Exit

Traditional approaches

a goto L here is the same control flow as exception handling (catching at the label L)

Longjmp and setjmp -- src
Exception handling

so dynamic multi-level exit allows complex forms of transfers among routines, this is often called exception handling

Static/Dynamic Call/Return
return call
static dynamic
static sequel termination exception
dynamic routine routine pointer, virtual routine, resumption

static return means that where the control flow is transferred after a function call is known statically (without running)

dynamic return means the location to return to (the caller) isn't known until code is run. In other words, there are multiple function callers.

static call means you know what function will be run when calling foo()

dynamic call means we don't know what code block will be run when calling a function.

Resumption

termination is recovery, resumption says there might be a fix without needing to unwind the stack

Micro C++

why is there no _At for _Throw?

3 Coroutine

Any routine that can be suspended and resumed

Let's look at a Fibonacci number generator, nextFibonacci()

Nonlocal Exceptions
Full Coroutines

Life cycle
  1. start the cycle
  2. execute the cycle
  3. stopping the cycle to return to the program ::main

Coroutine Languages

two types of coroutine languages: stackless and stackful (powerful)

5 Concurrency

5.3 Concurrent Hardware
5.4 Execution States

5.5 Threading Model

5.6 Concurrent Systems

three types:

  1. discovered concurrency, derived from a sequential program (only some programs)
  2. implicit constructs (compiler decides how many threads to run)
  3. explicit constructs, the important one to understand

1 and 2 build on 3.

It turns out there is no single model for having all concurrent capabilities

5.8 Concurrency

Requires 3 mechanisms in a programming language

  1. creation
  2. synchronization; timing relationships (A happens before B, etc..)
  3. communication; data transmission

5.9 Thread Object

C++ object that represents a thread: _Task

5.10 Termination Synchronization
5.11 Divide and Conquer
5.12 Synchronization
5.13 Communication
5.14 Exceptions
5.15 Critical Section
5.19 Software Solutions for Mutex

Locks

6.3.3 Barrier

coordinates a group of tasks performing a concurrent operation sandwiched between sequential code

6.3.4 Semaphore

Process graph != precedence graph

7 Concurrent Errors

8 Indirect Communication

9 Direct Communication

10 Optimization