13

Chapter 13 — Fibers vs. Threads, and the Guard Page

A fiber and an OS thread are the same shape of worker at roughly two orders of magnitude different memory cost per unit.

Demo 13Source on GitHub ↗

The full source for this example lives in examples/concurrency/13-fiber-worker-pool/ — clone the repo, cd in, and ./demo.sh.

Chapter reference: Doc 13 — Fibers and Stackful Coroutines

What it shows

One worker_loop() function, shared verbatim by both modes, pops work off a boost::fibers::buffered_channel — once run as Boost.Fiber fibers cooperatively sharing a single OS thread, and once run as independent std::thread workers — so the comparison is the same shape of worker at two different units of concurrency, not two different programs. Correctness and peak resident memory are both reported for each mode; the RSS gap is informational, not a fixed asserted ratio, since allocator behavior varies by host. A separate, safely contained piece forces an unbounded recursive function to walk into a fiber’s PROT_NONE guard page inside a forked child process, producing an immediate, deterministic SIGSEGV instead of corrupting whatever memory happened to sit past the stack.

Running it

cd examples/concurrency/13-fiber-worker-pool
./demo.sh            # build + run fiber_worker_pool (both modes + guard-page) + verify.lua
./demo.sh --clean    # remove the image