Skip to content
Tutorials Center
My learning

Topics

Lesson 80 of 132 ยท Concurrency

Parallel Patterns Without Libraries

Split work across threads using only std: thread::scope with chunks, parallel sum and map, chunks_mut, a work queue, a hand-built thread pool, and how Rayon does it.

You now know the pieces: threads, channels, locks, atomics and the Send and Sync rules. This lesson puts them together into the patterns that actually speed programs up. Most real parallel code is not clever. It follows one of a handful of shapes: split the data, work on the pieces independently, combine the answers. Everything in this lesson uses only the standard library, so you will see exactly what a library such as Rayon does for you, and when you can do without it.

Our online editor gives your program a single CPU core, so these examples show correct structure, not speed-ups. We never print timings, and every output is deterministic. On your own machine, std::thread::available_parallelism() tells you how many threads can really run at once; it is the usual starting point for choosing a thread count.

First, should you parallelize?

Parallelism has a cost: starting threads (tens of microseconds each), splitting and combining data, and coordination. It pays off when:

  • the work is CPU-bound (computing, not waiting for the network or disk),
  • it can be split into independent pieces,
  • each piece is big enough to outweigh the overhead (think milliseconds, not nanoseconds),
  • and you measured the sequential version first and it is actually too slow.

Amdahl's law puts a ceiling on the gain: if 10 percent of your program is inherently sequential, no number of cores will make it more than 10 times faster. Also remember that waiting on I/O is a different problem; for thousands of concurrent connections, async is usually a better fit than threads.

Pattern 1: split a slice into chunks

Scoped threads (thread::scope) can borrow local data, so you can hand each thread a slice of the same Vec without Arc or cloning. The slice method chunks(n) yields consecutive pieces of at most n elements.

Rust Parallel sum with scoped threads

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Look at the shape: div_ceil (stable since 1.73) computes the chunk size, each thread returns a number through its JoinHandle, and the parent sums them. No shared mutable state exists at all, so there is nothing to lock. This is the map-reduce pattern, and when you can express your problem this way it is the best option: no locks, no atomics, no contention.

max(1) protects against chunks(0), which panics. With an empty slice, chunks simply yields nothing and the sum is 0.

Do not collect handles lazily. If you wrote .map(|c| scope.spawn(...).join().unwrap()) in one iterator chain, each thread would be joined before the next starts and you would get sequential execution. Collect all handles into a Vec first, then join.

Pattern 2: transform in place with chunks_mut

When each element is computed from itself, you can give every thread a disjoint mutable slice. The borrow checker accepts this because chunks_mut hands out non-overlapping &mut [T] pieces, and each piece goes to exactly one thread.

Rust Parallel map with chunks_mut

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Four threads write to four different parts of the same vector at the same time, with no synchronization, and it is safe. This is the part of fearless concurrency that surprises people: splitting a &mut into disjoint pieces is something the type system can reason about. If you tried to give two threads overlapping slices, the compiler would refuse (see Send and Sync for the matching error).

The same trick works for any slice: split_at_mut gives two halves, chunks_exact_mut gives equal pieces, and a divide-and-conquer sort can recurse on both halves in parallel:

Rust Parallel merge sort with split_at_mut

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

The depth parameter limits how many levels spawn threads. Without it, a million-element sort would start hundreds of thousands of threads. Always cap the amount of parallelism; spawning cost grows quickly and threads are not free (each reserves stack memory).

Pattern 3: a shared work queue

Chunks of equal size work when every item costs the same. If some items are much slower than others, one thread finishes early and sits idle while another is still busy. A better approach is dynamic scheduling: workers repeatedly grab the next unprocessed item. The simplest queue is an atomic index into a shared slice.

Rust Dynamic scheduling with an atomic index

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

fetch_add hands out each index to exactly one worker, so no job runs twice and none is skipped. A worker that draws a cheap job simply comes back for another sooner. Each worker keeps its own Vec of results (no locking), and we merge them after the join. We sort at the end because the assignment of jobs to threads changes from run to run.

The let ... else (stable since 1.65) is a neat way to say "take the next job or leave the loop".

Pattern 4: a thread pool by hand

Starting a thread for every tiny task is wasteful. A thread pool starts N workers once and feeds them jobs through a channel. The standard library has no pool, but building a small one is a classic exercise that ties together channels, Arc<Mutex<_>> and trait objects.

A mpsc::Receiver is not Sync and cannot be cloned, so to let several workers pull from it we wrap it in Arc<Mutex<Receiver<Job>>>. A job is a boxed closure: Box<dyn FnOnce() + Send + 'static>.

Rust A minimal thread pool

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

How it fits together, step by step:

  1. execute boxes the closure and sends it into the channel.
  2. Each idle worker locks the receiver, calls recv() (which blocks until a job arrives), then lets go of the lock before running the job. Holding the guard during job() would serialize all work, a mistake that is easy to make by writing while let Ok(job) = receiver.lock().unwrap().recv() { job() }: in that form the temporary guard lives for the whole loop body.
  3. When the pool is dropped we close the channel by dropping the sender. Every worker's recv() then returns Err, the loop breaks, and drop joins the threads. This is a clean graceful shutdown.

Results travel back over a second channel, and we sort because completion order is not deterministic. The sorted output is always 1! = 1 up to 8! = 40320.

Real pools add panic handling (a panicking job should not kill its worker), a bounded queue (sync_channel) so producers cannot flood memory, and work stealing between per-thread queues for better balance. That is why you normally reach for a crate.

Pattern 5: fork and join with return values

Sometimes the problem is recursive, such as walking a tree or splitting a range. Scoped threads make "spawn one half, compute the other half yourself, then combine" easy:

Rust Recursive fork and join

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

The test that matters here is the last line: the parallel and sequential versions must agree. Whenever you parallelize, keep the sequential version as a reference and compare results in tests. Subtle differences (tie-breaking, floating-point addition order) are the usual cause of mismatches.

Floating-point addition is not associative. A parallel sum of f64 values adds them in a different order than a sequential sum and can give a slightly different answer. That is not a bug in Rust; decide whether the difference matters and compare with a tolerance.

Pitfalls that cost people days

  • False sharing. Threads writing to neighbouring elements in a Vec<u64> (for example counters indexed by thread number) fight over one cache line and can run slower than one thread. Give each thread a local variable and write the result once.
  • A lock in the hot loop. Locking per item makes the lock the bottleneck. Accumulate locally, merge at the end.
  • Too many threads. More threads than cores adds scheduling overhead and memory. Start with available_parallelism().
  • Unbounded queues. A fast producer and slow workers will exhaust memory unless the queue is bounded.
  • Panics. A panic inside a scoped thread is re-raised by scope when it ends; a panic in a detached thread is only noticed through join. Decide how your program reacts.
  • Output order. Anything printed from several threads interleaves unpredictably. Collect results, then print.

Rayon: what a library gives you

Rayon is the standard answer to data parallelism in Rust. It keeps a global pool, uses work stealing, and turns an iterator chain into a parallel one by changing a single word. The following snippet needs the rayon crate, so it cannot run in our editor; it is shown read-only:

TOML
# Cargo.toml (read-only: needs the rayon crate, not available in the editor)
[dependencies]
rayon = "1"
Rust
// Read-only: requires the rayon crate.
use rayon::prelude::*;

fn main() {
    let numbers: Vec<u64> = (1..=1_000_000).collect();

    // Sequential:
    let a: u64 = numbers.iter().map(|n| n * n % 7).sum();

    // Parallel: iter() became par_iter(). Rayon splits the work itself.
    let b: u64 = numbers.par_iter().map(|n| n * n % 7).sum();

    assert_eq!(a, b);
}

Because the closure passed to par_iter must be Send + Sync, the compiler applies exactly the checks from the previous lesson. Everything Rayon does can be expressed with the tools in this lesson, but Rayon does it with a well-tested pool, adaptive splitting and a much nicer API. A good rule: write the scoped-thread version when you have one or two simple parallel loops; reach for Rayon when parallelism spreads through your code. If your work waits on the network instead of computing, look at async instead.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why can scoped threads borrow a local Vec without Arc or move?

    thread::scope joins every thread before returning. The compiler can therefore prove that borrowed data outlives all the threads.
  2. 2. What is the main advantage of splitting a slice with chunks_mut for parallel work?

    The pieces do not overlap, so the borrow checker accepts handing each to a different thread. There is no shared mutable state to protect.
  3. 3. In the hand-built thread pool, how do the workers know it is time to stop?

    Dropping the Sender closes the channel. recv() then returns Err, which the worker treats as the signal to break out and finish.
  4. 4. Why is an atomic index (fetch_add) a good work queue for uneven jobs?

    fetch_add gives each index to exactly one caller. Workers that finish early come back for more, which balances the load dynamically.
  5. 5. Which is a common reason a parallel version is slower than the sequential one?

    Thread start-up, locking in the hot path, false sharing and too little work per task can outweigh the gain. Measure, and keep shared state out of inner loops.

Summary

  • Parallelize CPU-bound, independent work that is large enough to outweigh the overhead, and measure first; Amdahl's law caps the gain.
  • thread::scope with chunks gives map-reduce without Arc: spawn one thread per chunk, collect the handles, then join and combine.
  • chunks_mut and split_at_mut hand disjoint &mut slices to threads, so in-place parallel work needs no locks.
  • Use an atomic index (or a channel) as a work queue for uneven jobs; sort results before printing because completion order varies.
  • A thread pool is workers plus a channel of boxed FnOnce jobs; dropping the sender shuts it down cleanly.
  • Cap the number of threads, avoid locks in hot loops, keep a sequential reference version, and consider Rayon for data-parallel iterator code.

Next: Async/await Basics