How lwhyz/workspace works
1. Repository shape
This is a header-only C++11 asynchronous execution framework called workspace. The public API lives under include/workspace/, with small standalone programs in example/, benchmarks in bench/ and benchmark/, and unit tests in test/. Build and packaging are CMake-based.
2. Top-level architecture
The framework has three core runtime layers:
- workbranch: an executable task pool / worker-branch abstraction
- supervisor: a control plane that monitors and adjusts workbranch capacity
- workspace: a container/dispatcher that owns and routes between multiple workbranches and supervisors
Around these sit supporting utilities like a future collector and internal thread/task helpers.
3. workbranch: the execution engine
workbranch is the primary data plane. It owns a thread-safe task queue and a set of worker threads that continuously pull tasks and execute them. It supports:
- async submission with or without return values
- task priorities (normal vs urgent)
- batched “sequence” tasks that are executed serially by one worker
- waiting for all queued/in-flight tasks to finish
- exception handling, either logged locally or propagated via std::future
4. Task flow through workbranch
A task enters through submit(...), is wrapped into the framework’s task abstraction, placed into the internal queue, then consumed by a worker thread. If the task returns a value, the submission path produces a std::future; otherwise it is fire-and-forget. For exception-bearing tasks, the framework either logs the exception inside the worker or stores it in the future for later retrieval on the caller side.
5. Scheduling and wait strategies
Worker idle behavior is configurable via three waiting modes:
- lowlatancy: busy-wait / yield for minimal latency
- balance: spin first, then sleep briefly
- blocking: sleep on a condition variable until work arrives or shutdown/wait conditions change
This is a meaningful architectural boundary because it trades off latency, CPU usage, and contention under different workloads.
6. supervisor: dynamic control plane
supervisor is a monitoring and auto-scaling layer for one or more workbranches. It observes load and adjusts the number of worker threads within configured bounds. It can also run a periodic “tick callback” after each adjustment, which is intended for logging or custom control logic. In the architecture diagram, this is the feedback loop between observed queue pressure and worker pool size.
7. workspace: ownership and dispatch hub
workspace is a manager/dispatcher that owns multiple workbranch and supervisor instances via internal unique ownership. It assigns component IDs, provides indexed access, and coordinates task distribution across branches. Its main architectural role is to prevent lifetime hazards, reduce cross-component contention, and provide simple load balancing across managed branches.
8. Cross-branch task distribution
When a task is submitted to workspace, it is dispatched to one of the managed workbranch instances according to the framework’s balancing logic. This is the main boundary between orchestration and execution: workspace decides *where* work goes, while workbranch decides *when and how* worker threads run it. The benchmarks suggest this dispatch layer reduces contention when branches are configured with fewer worker threads.
9. Auxiliary future collector
wsp::futures<T> is a small batching utility for collecting many futures of the same result type. It supports storing futures, waiting on all of them, and extracting their values as a vector. This is a convenience layer above the core execution engine and is not a scheduler itself.
10. Lifecycle and safety boundaries
The repo emphasizes ownership and thread-safety boundaries:
- workbranch and supervisor are thread-safe
- workspace is not thread-safe
- futures is not thread-safe
A key design constraint is that workspace should own and outlive its managed components to avoid dangling references, especially between workbranches and supervisors.
11. Examples, tests, and benchmarks
The example/ programs demonstrate the main execution patterns: basic async work, urgent tasks, sequence batching, exception propagation, supervisor-driven scaling, and workspace-managed multi-branch dispatch. The test/ directory covers core behavior such as function execution, exception handling, supervisor logic, workbranch behavior, and workspace behavior. The two benchmark areas measure queue/synchronization overhead and idle-wait latency under different scheduling strategies.
12. External/runtime dependencies
The library targets standard C++11 and is cross-platform in principle, but its runtime model depends on native threading (std::thread / pthread-style behavior) and std::future/std::condition_variable. There are no external services; the architecture is entirely in-process concurrency and scheduling.
Main entry points: workbranch.hpp workbranch · example examples · test tests · CMakeLists.txt cmake
Components 17
Core runtime
Execution, control, and dispatch layers.
- workspace · Top-level manager that owns multiple branches and supervisors, dispatches work across them, and provides indexed access.
include/workspace/workspace.hpp - supervisor · Monitors load and adjusts worker capacity within configured bounds, with optional tick callbacks.
include/workspace/supervisor.hpp - workbranch · Primary execution engine: owns worker threads and a task queue, submits async work, and waits for completion.
include/workspace/workbranch.hpp - task queue · Thread-safe queue backing workbranch task intake and worker consumption.
include/workspace/taskqueue.hpp - async submit · Fire-and-forget and value-returning submission paths, including exception propagation through futures.
- wait modes · Low-latency, balanced, and blocking worker idle policies.
- sequence tasks · Serialized task batches executed in order by a single worker.
- exceptions · Local logging or future-based propagation of task exceptions.
Support
Shared utilities used by the runtime.
- utility · Shared internal helpers for task wrapping, threading, and execution glue.
include/workspace/utility.hpp - futures · Batches homogeneous futures, waits on them, and extracts values as a vector.
- threading · Uses std::thread, std::future, and std::condition_variable for in-process concurrency.
Examples
Standalone usage programs.
- examples · Standalone programs demonstrating async work, priorities, batching, scaling, and multi-branch dispatch.
example
Validation
Tests and benchmarks.
- benchmarks · Measures queue/synchronization overhead and idle-wait latency across scheduling strategies.
bench - benchmark · Additional benchmark harnesses for runtime behavior and waiting strategies.
benchmark - tests · Core behavior tests for function execution, exceptions, supervisor logic, workbranch, and workspace.
test
Connections 24
- workspace → workbranch: owns/dispatches · Workspace manages multiple branches and routes work to one of them.
- workspace → supervisor: owns/coordinates · Workspace keeps supervisors with the branches they regulate.
- supervisor → workbranch: scales · Supervisor observes load and adjusts worker count on a branch.
- workbranch → task queue: uses · Workbranch submits to and drains from its internal task queue.
- workbranch → async submit: implements · The public submission surface is provided by workbranch.
- async submit → exceptions: routes · Submission paths either log exceptions or propagate them through futures.
- workbranch → sequence tasks: supports · Workbranch executes batched sequence tasks serially on one worker.
- workbranch → wait modes: configures · Idle worker behavior is controlled by the chosen wait strategy.
- workbranch → threading: depends on · Execution relies on native threading and synchronization primitives.
- workbranch → utility: uses · Helper utilities support task wrapping and internal execution glue.
- futures → threading: depends on · Future collection uses standard future synchronization semantics.
- futures → async submit: collects outputs · Collects many result-bearing submissions and extracts their values.
- examples → workspace: demonstrates · Examples show workspace-managed dispatch across branches.
- examples → workbranch: demonstrates · Examples cover async work, urgency, batching, and waiting behavior.
- examples → supervisor: demonstrates · Examples include supervisor-driven scaling behavior.
- tests → workspace: verifies · Tests cover workspace behavior and access patterns.
- tests → workbranch: verifies · Tests cover execution, waiting, and exception handling.
- tests → supervisor: verifies · Tests cover supervision and scaling logic.
- benchmarks → workbranch: measures · Benchmarks target queueing and synchronization overhead in the executor.
- benchmark → wait modes: measures · Benchmark harnesses compare idle-wait latency strategies.
- cmake → examples: builds · CMake config builds the example programs.
- cmake → tests: builds · CMake config builds the test suite.
- cmake → benchmarks: builds · CMake config builds benchmark targets.
- docs → cmake: documents · Repository docs describe build and usage entry points.