affinitypool
A threadpool for running blocking jobs on a dedicated thread pool. Blocking tasks can be sent asynchronously to the pool, where the task will be queued until a worker thread is free to process the task. Tasks are processed in a FIFO order.
Tasks are delivered through a sharded, lock-free queue. Each producer thread routes consistently to its own shard, so concurrent producers' traffic stays isolated and contention stays low, while idle workers steal across shards to stay busy.
Examples
Basic Usage
Create a threadpool and spawn tasks that run on worker threads:
use Threadpool;
async
Using the Builder
Configure the threadpool with custom settings:
use Builder;
async
Thread per core
Spawn one worker thread per CPU core. This sets the worker count to the number of available cores; thread placement is left to the OS scheduler (workers are not pinned):
use Builder;
async
Global Threadpool
Set up a global threadpool that can be accessed from anywhere:
use ;
async
async
Local Spawning
Use spawn_local when you need to borrow data without the 'static lifetime requirement.
spawn_local is unsafe: the closure may borrow non-'static data, and that borrow stays sound only as long as the returned future is not leaked (mem::forget, Box::leak, an Rc/Arc cycle, …) before the borrow ends. Awaiting it — or simply letting it drop — upholds the contract; leaking it while it borrows local data is a use-after-free. This cannot be enforced statically in async Rust, which is why the API is unsafe rather than safe; if your closure only captures 'static data, prefer the safe spawn.
use Threadpool;
async
Handling Multiple Concurrent Tasks
Process multiple blocking tasks concurrently:
use Threadpool;
use ;
async
Benchmarks
Head-to-head against the most common alternatives for running blocking work in async Rust:
tokio::task::spawn_blocking— Tokio's built-in blocking pool.blocking::unblock— the auto-scaling pool used byasync-stdand the smol ecosystem.rayon::ThreadPool::spawn— Rayon's work-stealing pool. Tasks are wrapped in atokio::sync::oneshotso the producer can await; that handshake is part of what's measured.threadpool::ThreadPool::execute— the crate this library was originally forked from. Sameoneshotwrap as Rayon.
Three workloads run against each pool: spawn_overhead (submit N closures, await each), round_trip (submit-and-await one closure at a time), and multi_producer (P concurrent producers each pushing 1k tasks). Numbers are criterion midpoint estimates from --quick runs on a quiet Linux bench machine. Bold = affinitypool is the fastest in the row.
| Benchmark | affinitypool | tokio | blocking† | rayon | threadpool |
|---|---|---|---|---|---|
spawn_overhead/1w/1 |
5.84 µs | 6.36 µs | 2.49 µs | 1.03 µs | 5.76 µs |
spawn_overhead/4w/1 |
6.77 µs | 5.71 µs | 2.49 µs | 1.53 µs | 6.54 µs |
spawn_overhead/1w/100 |
18.0 µs | 35.7 µs | 181 µs | 20.7 µs | 27.1 µs |
spawn_overhead/4w/100 |
98.8 µs | 87.3 µs | 181 µs | 39.5 µs | 27.2 µs |
spawn_overhead/1w/1000 |
208 µs | 286 µs | 1.86 ms | 270 µs | 278 µs |
spawn_overhead/4w/1000 |
875 µs | 1.19 ms | 1.86 ms | 332 µs | 240 µs |
spawn_overhead/1w/10000 |
2.26 ms | 2.40 ms | 21.3 ms | 2.20 ms | 2.09 ms |
spawn_overhead/4w/10000 |
12.0 ms | 10.4 ms | 21.3 ms | 3.37 ms | 2.19 ms |
round_trip/1w |
5.54 µs | 5.76 µs | 2.49 µs | 1.01 µs | 4.45 µs |
round_trip/4w |
6.19 µs | 6.57 µs | 2.49 µs | 1.44 µs | 6.24 µs |
round_trip/8w |
6.56 µs | 5.37 µs | 2.49 µs | 2.24 µs | 6.78 µs |
multi_producer/2p_1w |
367 µs | 409 µs | 4.88 ms | 524 µs | 385 µs |
multi_producer/2p_4w |
1.12 ms | 1.73 ms | 4.88 ms | 731 µs | 426 µs |
multi_producer/4p_1w |
1.00 ms | 1.16 ms | 11.2 ms | 973 µs | 890 µs |
multi_producer/4p_4w |
1.16 ms | 1.80 ms | 11.2 ms | 938 µs | 976 µs |
multi_producer/8p_1w |
3.97 ms | 3.29 ms | 27.3 ms | 1.94 ms | 2.35 ms |
multi_producer/8p_4w |
1.61 ms | 5.42 ms | 27.3 ms | 1.73 ms | 1.94 ms |
† blocking uses a single auto-scaled global pool; its column doesn't vary with the worker count.
How affinitypool compares
- vs
tokio::spawn_blocking— affinitypool wins or ties on most workloads, including a 3.4× lead onmulti_producer/8p_4w. Tokio still leads on the single-task4wcases. - vs
blocking::unblock— affinitypool dominates batched workloads (5–14× faster) and loses single-task latency (blockingis consistently ~2.5 µs). Trade-off:blocking's pool grows unboundedly and is shared globally with any other crate using it. - vs
rayon::ThreadPool::spawn— Rayon's lock-free deques win single-task latency (3–6×) and most4wbatched workloads. affinitypool wins onmulti_producer/8p_4wand ties or wins most1wworkloads. Rayon is built for work-stealing CPU parallelism, not async producer / worker handoff. - vs
threadpool::ThreadPool::execute— the original. Parity on1wworkloads, threadpool wins big (4–5×) on4wbatched spawn (no sharding overhead), affinitypool wins onmulti_producer/8p_4wand2p_1w.
The pattern: affinitypool loses to the single-queue and work-stealing alternatives on 4w/N batched-spawn workloads where one producer fans out fast and pays for crossing shards. It wins where shard locality pays back — 1w (no scan cost), and multi-producer workloads where each producer lands on its own shard via its thread-ID hash.
Unlike a single shared queue, affinitypool gives you a dedicated pool sized for blocking work with per-producer shard affinity — concurrent producers stay isolated on their own shards, which is where it wins.
Architecture
Tasks are delivered from producers to workers through a sharded MPMC queue. Each producer routes to a shard via a cached hash of its thread ID, so a given producer consistently lands on the same shard (hash & mask). Each worker has a preferred shard (worker_idx & mask) and falls back to scanning the remaining shards in cyclic order before parking.
Producers (any async task)
+----------------+ +----------------+ +----------------+
| producer @ c0 | | producer @ c1 | | producer @ cN |
+----------------+ +----------------+ +----------------+
| | |
v v v
Sharded queue (num_workers.next_power_of_two().min(8))
+----------------+ +----------------+ +----------------+
| Shard 0 | | Shard 1 | | Shard k |
| Mutex< | | Mutex< | | Mutex< |
| VecDeque< | | VecDeque< | | VecDeque< |
| Runnable>> | | Runnable>> | | Runnable>> |
+----------------+ +----------------+ +----------------+
| | |
v v v
Worker threads
+----------------+ +----------------+ +----------------+
| worker 0 | | worker 1 | | worker k |
| pref: shard 0 | | pref: shard 1 | | pref: shard k |
+----------------+ +----------------+ +----------------+
On an empty preferred shard a worker scans the remaining shards in cyclic order, then parks on a shared Mutex<()> + Condvar (counted by an AtomicUsize). Producers check that counter after pushing; if any worker may be parked, they briefly take the park mutex to notify_one.
Each task is a single heap allocation (the async-task layout — fused header + closure + result slot + waker). The park/unpark handshake is lost-wakeup-free; the proof sketch lives in src/queue.rs and the model in tests/loom_queue.rs.
Shard count rules of thumb:
| Workers | Shards |
|---|---|
| 1 | 1 (no scan cost, no extra mutex) |
| 2–3 | 2–4 |
| ≥ 5 | 8 (capped) |
Behaviour notes
Worker self-spawn fast path. When a closure running on a worker thread calls pool.spawn(...), the new task is pushed directly into that worker's own local deque instead of routing through the shared sharded queue, skipping the shard routing. The spawning worker is usually also the consumer — it returns to its pop loop and drains its own deque — so the work stays biased toward that worker, which is what you want for cache locality.
It still issues the same wake handshake a foreign push does, because the spawning worker is not guaranteed to reach its pop loop: a worker that polls a SpawnFuture and then drops it blocks in the drop, waiting for the very runnable it just queued. That runnable is in the blocked worker's own deque, so only a peer steal can complete it — the spawning worker's deque is a stealer target. Skipping the wake there let the pool hang until the blocked worker gave up, which is forever. On a one-worker pool there is no peer to wake, so that pattern self-deadlocks regardless; see the Threadpool::spawn_local docs.
Original
This code is heavily inspired by threadpool, licensed under the Apache License 2.0 and MIT licenses. Earlier versions also included CPU-core-pinning code forked from core-affinity; that code has since been removed.