Expand description
task_pool
offers a flexible abstraction for composing and distributing work within a fixed hardware threadpool. To that end, it offers the following features:
- The ability to define and compose sources of work
- The ability to create hardware threadpool and consume those sources
- A variety of high-level abstractions for scheduling, such as awaitable tasks
§Usage
To use task_pool
, there are three steps:
- Creating and initializing
WorkProvider
instances (such as a queue or chain of multiple queues) - Creating a hardware
TaskPool
which consumes those instances - Spawning high-level tasks on the
WorkProvider
s which are handled by the threadpool
The following example shows these steps in action:
// 1. Create a queue from which we can spawn tasks
let queue = TaskQueue::<Fifo>::default();
// 2. Create a threadpool that draws from the provided queue. Forget the threadpool so that it runs indefinitely.
TaskPool::new(queue.clone(), 4).forget();
// 3. Spawn a task into the queue and synchronously await its completion.
assert_eq!(queue.spawn(once(|| { println!("This will execute on background thread."); 2 })).join(), 2);
// ...or, asynchronously await its completion.
assert_eq!(queue.spawn(once(|| { println!("This will execute on background thread."); 2 })).await, 2);
Structs§
- Chained
Work Provider - A provider which multiplexes work units from other providers, in a fixed priority order.
- Change
Notification Listener - Manages the lifetime of a registered change notification callback. Upon drop, the associated callback will no longer be invoked.
- Change
Notifier - Offers a composable way to listen for the availability of new work from providers.
- Fifo
- Marks a task queue as executing events in a first-in-first-out order.
- Lifo
- Marks a task queue as executing events in a last-in-first-out order.
- Priority
- Marks a task queue as executing events in a user-defined priority order.
- Task
- A handle to a queued group of work units, which output a single result.
- Task
Pool - Controls a set of background threads that execute work from a provider.
- Task
Queue - Sends ordered tasks to a pool for background processing.
Traits§
- Task
Collection - Represents a task that returns a result of the given type.
- Task
Provider - Provides a group of work units that compose a task.
- Work
Provider - A persistent source of work for multiple threads.
- Work
Unit - A single, atomic unit of work that one thread should process.