A thread pool
Stage 8 of 8v2 · ad0bfe2a

Work stealing and metrics

Use local queues, steal from the opposite end, and report comparable contention.

Work stealing and metrics

Use local queues, steal from the opposite end, and report comparable contention. Preserve earlier capabilities and use the fixture only to make observation deterministic.

Contract

The harness receives pure tasks with simulated duration and result. The implementation must use real threads, while output is sorted by ID to remove scheduler variation.

{ "op": "rebalance", "queues": [[0, 1, 2], [], [3]], "idleWorker": 1 }

produces exactly:

{ "stealFrom": 0, "task": 0, "queueSizes": [2, 1, 1] }

Emit compact JSON with a final newline. Invalid input leaves stdout empty, writes error to stderr, and exits non-zero.

Design and limits

Separate policy, mutable state, and system effects. Make resource ownership, cleanup order, and pre/post-operation invariants explicit. Do not replace the central mechanism with a simulation: the fixture controls inputs and time while your code implements isolation, memory, or synchronisation as appropriate.

Test empty boundaries, mid-operation failures, and idempotent cleanup. Sort output only at the presentation boundary.

Acceptance

  • The example produces the exact output.
  • Resources are not leaked and work is not duplicated.
  • Failure leaves the system coherent.