Skip to main content

Module threading

Module threading 

Source
Expand description

A small, fixed-size worker pool for offloading CPU-bound work off the main thread — mip/image processing, physics steps, any one-off or recurring task that shouldn’t block a frame.

Deliberately NOT a full async runtime: no cancellation, no priorities, no work-stealing. Just a bounded number of OS threads pulling jobs off one shared, lock-free MPMC queue, with results delivered back via a channel you poll from an ordinary system. If you outgrow this — need cancellation, need priority scheduling — that’s real, separate infrastructure to build once you have a concrete case for it, not something to guess at now.

Requires the crossbeam-channel crate (lock-free MPMC), since std::sync::mpsc only supports a single consumer and would otherwise force a Mutex around the receiver for multiple worker threads.

Structs§

BackgroundTasks
The worker pool itself. Insert as a resource once, at startup; every system that needs to offload work reaches for Res<BackgroundTasks> and calls spawn.
BackgroundTasksPlugin
Registers BackgroundTasks as a resource with the given worker count.
TaskHandle
A handle to a single in-flight (or already-finished) task. Poll it from an ordinary system with try_recv — never blocks.