Concurrency
Actor Model
Is a computational model that describes everything as an Actor. These actors have 3 possible tasks:
- Create another Actor
- Send messages to other Actors
- Determine the behavior/state for the next message it receives.
Some applications
- Email with accounts, messages and addresses.
- REST APIs, with endpoints working as addresses.
Interrupt Handlers
Are one of the first concurrent programs 1 . At the lowest level, these interrupts come in the form of hardware signals with high-priority. This interaction of the hardware telling something to the CPU, is what we refer to as interrupts2.
The implemented interrupt handler then requires a specific interrupt flag to react accordingly.
Mutual Exclusion Solutions
First of all, a mutex is just a programming concept referring to how a thread, and only that thread, has access to a specific set of resources at a time.
- Dekker’s Algorithm 3
1begin integer c1, c2 turn;
2 c1:= 1; c2:= 1; turn = 1;
3 parbegin
4 process 1: begin A1: c1:= 0;
5 L1: if c2 = 0 then
6 begin if turn = 1 then goto L1;
7 c1:= 1;
8 B1: if turn = 2 then goto B1;
9 goto A1
10 end;
11 critical section 1;
12 turn:= 2; c1:= 1;
13 remainder of cycle 1; goto A1
14 end;
15 process 2: begin A2: c2:= 0;
16 L2: if c1 = 0 then
17 begin if turn = 2 then goto L2;
18 c2:= 1;
19 B2: if turn = 1 then goto B2;
20 goto A2
21 end;
22 critical section 2;
23 turn:= 1; c2:= 1;
24 remainder of cycle 2; goto A2
25 end
26 parend
27end .
- Peterson’s Algorithm4
1bool flag[2] = {false, false};
2int turn;
3
4P0: flag[0] = true;
5P0_gate: turn = 1;
6 while (flag[1] && turn == 1)
7 {
8 // busy wait
9 }
10 // critical section
11 ...
12 // end of critical section
13 flag[0] = false;
14
15P1: flag[1] = true;
16P1_gate: turn = 0;
17 while (flag[0] && turn == 0)
18 {
19 // busy wait
20 }
21 // critical section
22 ...
23 // end of critical section
24 flag[1] = false;
Types of Mutex Devices
Semaphores
Semaphores “hold” the resources available and lends them to threads so they can access them for a limited period of time, while other threads wait for their required resources to be available 5.
A good read about semaphores is Julie Zelenski’s “Threads and Semaphores Example”6, outlining different examples on how semaphores are used, and the separation between a general and a binary semaphore.