RustcargotokioAdvanced~4h

Rust: Concurrency

Four designs, one answer

A concurrent program prints something that depends on how the machine felt that second. This project is about the other half: what stays the same however the work was split. You build one program four ways — threads, a channel, a shared map, async tasks — and make all four agree with each other, and with arithmetic.

Modules
5
Drills
7
Build steps
5
Time
~4h

Walk out able to

Choose between sending a value and sharing one, and say which trait, which lock and which missing `.await` decides whether it compiles.

What you build and drill

01

Threads that finish

Spawn, join, and rule out a run that cannot happen.

02

What may cross

Send and Sync, read off a type before the compiler says so.

03

Send it or share it

Channels, one shared map, and locks that let go on time.

04

Async, and the clock

Tasks instead of threads, and what overlaps before it runs.

The pitch

What you practise, and what you leave with

A four-mode counting program with an acceptance test: every design agrees with every other at every thread count, and with a paper answer.

You will practise

  • Rule out a run a program cannot produce
  • Read Send and Sync off a concrete type
  • Say when a receiver stops, and when it hangs
  • Predict an async program’s clock before running it

Afterwards you can

  • Split work across threads and get it back
  • Choose between sending and sharing
  • Hold a lock for exactly the right span
  • Say what async bought, honestly

Modules

5 modules, 31 items

Lessons explain one idea. Drills repeat it until it sticks. Build steps make something that exists afterwards.

  1. 1

    Two threads, one program

    ~45 min

    Start threads, join them, and say which outputs a run could and could not produce.

    • LessonWhy build one program four ways4 min
    • LessonWhat spawn hands back6 min
    • LessonGive a thread something to work on6 min
    • LessonRule a run out5 min
    • DrillRule out a run that cannot happen10 min
    • Build stepSplit one total across four threads14 min
  2. 2

    What is allowed to cross

    ~39 min

    Read Send and Sync off a type, and predict which sharing the compiler will refuse.

    • LessonWhy the two markers exist7 min
    • LessonRead a wrapper from the inside6 min
    • LessonRead which marker was refused5 min
    • DrillName the marker a type is missing11 min
    • Build stepMake the compiler refuse, twice10 min
  3. 3

    Send it, don't share it

    ~43 min

    Move results home over a channel, and say exactly when the receiver stops.

    • LessonSend it and it is gone5 min
    • LessonOne receiver, many senders6 min
    • LessonSay when a receiver stops6 min
    • DrillCount what comes out of the channel11 min
    • Build stepCollect results over a channel15 min
  4. 4

    When sharing is the answer

    ~58 min

    Share one structure across threads, and hold the lock for the span the work needs.

    • LessonWhy it takes two wrappers5 min
    • LessonThere is no unlock5 min
    • LessonWhere the span runs long6 min
    • LessonShare only when you must5 min
    • DrillSay whether the lock lets go12 min
    • DrillChoose the smallest design that works10 min
    • Build stepTally into one shared histogram15 min
  5. 5

    Waiting without blocking

    ~1h

    Run the same work on tasks, and say what the clock will read before you run it.

    • LessonCalling it does nothing4 min
    • LessonThree ways to have two waits5 min
    • LessonThis is not a faster thread5 min
    • LessonRead a program for its clock4 min
    • DrillPredict what the clock will say11 min
    • Build stepRun the same work on tasks14 min
    • DrillType the concurrency you mean9 min
    • LessonShip the four-mode tally20 min

Four designs. One answer. Every run.

The first item is free. ~4h of focused work, at your own pace.

Advanced~4h5 modules