LightMySky

Message Passing and Asynchronous Work

Instead of sharing memory and guarding it, threads or processes can own their state and send messages. Queues, channels and event loops trade the race conditions of shared memory for questions about ordering, buffering and back pressure.

No account needed. Progress saves in this browser.

What a learner can do afterwards

  • Rewrite a lock-based counter as a single owner served by a message queue
  • Say what a bounded queue does when the producer outruns the consumer
  • Compare the failure modes of shared memory and of message passing

1 · Read

Sharing memory means every thread can touch the same counter, so you guard it with locks and inherit their costs. Message passing takes the other road: each piece of state gets one owner, and everyone else sends messages. The owner of a counter reads one message at a time and applies it. No shared counter means no race on it.

Try it together

Turn a lock-based counter into messages. One worker owns the count, starting at zero. Other workers send notes like add 1 or add 2 into the owner's queue. The owner handles the notes one by one: add 1 makes 1, add 1 makes 2, add 2 makes 4. The lock is gone because only the owner ever touches the count.

The queue between workers has a limited size, so you must decide what happens when a fast producer outruns a slow consumer. First the queue buffers the extra messages. When the buffer fills, the system pushes back: the producer waits its turn, or the queue drops or refuses new messages, depending on the policy you chose. Fast producers cannot outrun reality forever.

Good to know

Compare the failure modes before you pick a side. Shared memory fails with races and deadlocks around locks. Message passing removes those races on owned state, but adds new questions: a slow consumer, a full queue, dropped messages, and surprises in ordering. You trade one bug family for another.

Give each state one owner, send the rest as messages, size the queue, and pick which failure modes you would rather handle.

2 · Watch

Take it off screen

Print a worksheetA4 with an answer key page for grown-ups. No screen, no internet.

Where it sits

Then practise

8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.

Spotted a problem on this page? Tell us
Message Passing and Asynchronous Work · Computing, ages 21 to 22 · LightMySky