Skip to content

README: a barrier built from pt_wait() and pt_broadcast(), with a test - #30

Open
LarryRuane wants to merge 3 commits into
masterfrom
readme-barrier
Open

LarryRuane wants to merge 3 commits into
masterfrom
readme-barrier

Conversation

@LarryRuane

@LarryRuane LarryRuane commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

Adds an "Example - a barrier" section after "Wait channels". It shows a barrier for n protothreads, which with n of 2 is a rendezvous, in about a dozen lines on top of pt_wait() and pt_broadcast(). Waiters test a generation number, not the count. The last to arrive keeps running, and may arrive for the next round before the waiters it just woke get to run. A waiter testing the count would see that round and could wait forever. The README walks through the sequence, and why wrapping gen is harmless. Two threads can also swap data through it, but if they meet repeatedly they need two sets of slots, used in alternate rounds. The README now says that too.

The test suite gets a barrier section that uses the README code as a nested protothread function. Three threads go through five staggered rounds, and two threads exchange a value each round. Breaking either point (waiters testing the count, or a single slot) makes the test fail.

TODO's "Barrier and countdown latch" entry is now just the latch, since the README now shows a barrier.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SR7v9BVsFrHiEhQBSqZmAs

LarryRuane and others added 3 commits October 29, 2026 09:23
With n of 2 it's a rendezvous. The waiters test a generation number,
because the last to arrive keeps running and may start the next round
before they run; a waiter testing the count would see that round and
sleep forever. Checked against the library: the generation version
passes five rounds of three threads, and the count version hangs after
the first opening.

TODO keeps the countdown latch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SR7v9BVsFrHiEhQBSqZmAs
The barrier from README.md as a nested protothread function: three
threads, five rounds, staggered so the last to arrive changes, checking
that nobody passes early or gets more than a round ahead. Then two
threads exchanging a value each round through a barrier of 2.

The exchange needs two sets of slots, alternated by round. With one,
the thread that keeps running writes its next value before the other
has read this one; the README now says so. Mutations checked: one slot
fails the exchange check, and waiters testing the count instead of the
generation leave the threads stuck.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SR7v9BVsFrHiEhQBSqZmAs
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SR7v9BVsFrHiEhQBSqZmAs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant