Master the fundamental concepts of threads & concurrency through this focused micro-challenge.
Green threads are cooperatively scheduled coroutines living entirely in userspace. No kernel clone(), no preemption timer: each thread yields explicitly. Languages like early Ruby and Go prototypes used this model before OS threads matured.
Each green thread needs:
For example, three threads printing A, B, C with round-robin yields produce ABCABC... only if every thread calls yield() before looping again; forget one yield and the others starve.
cLoading…
Go's goroutine runtime, Ruby's Fiber, and pre-1.5 Java's green threads all started from exactly this design: cooperative user-space stacks switched with saved register state instead of kernel involvement. The single-kernel-thread limitation you'll hit here is precisely why early Java green threads were dropped in favor of native threads once multi-core CPUs became common in the early 2000s.
Before you call the implementation done, walk failure modes on purpose. Test empty structures, single-element edge cases, maximum concurrency, and errno paths that must not crash the program. OS code usually fails in production when happy-path tests pass but invariants break under contention or memory pressure.
Keep structures small and name fields after kernel counterparts when possible. That lets you read man pages and kernel source side by side while you work. Print observable events during development; remove noisy logs once tests pass reliably.
You will implement thread creation, cooperative yield(), and a scheduler dispatch loop. Building the stack switch yourself shows why modern runtimes moved to preemptive kernel threads for CPU-bound work.
Implement green threads using cooperative scheduling.
Requirements:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github