CS 452/652 Spring 2026 - Lecture 21

Multi-Core Programming / Overview

July 27, 2026 prev next

Parallelism

Atomicity

    Init:  A = 0

    Thread 1    Thread 2
    A += 1      A += 1

    Result: ?

Critical Section: Dekker's Algorithm (software locking)

    int wantIn[2] = {false, false};
    int turn = 0;
    int me = 0; // other thread: me = 1
    int you = 1 - me;
    wantIn[me] = true;
    while (wantIn[you]) {
        if (turn != me) {
            wantIn[me] = false;
            while (turn != me) { // wait }
            wantIn[me] = true;
        }
    }
    ... // critical section
    turn = you;
    wantIn[me] = false;

Critical Section: Atomic Operations

    int ticket = 0;
    int serving = 0;
    int myticket = ticket++;
    while (myticket != serving) { // wait }
    ... // critical section
    serving++;

Blocking vs. Spinning

Memory Ordering

    Init:  A = B = 0

    Thread 1    Thread 2
    A = 1       B = 1
    print(B)    print(A)

    Output: ?

Mechanisms

Sequential Consistency

    int myticket = ticket++;
    while (myticket != serving) { // wait }
    LOAD FENCE
    ... // critical section
    STORE FENCE
    serving++;

Additional Notes