๐Ÿด Fork-Joing Parallelism

= doing multiple independent tasks at once

Rust

Libraries

  • rayon: great for fork-join

Example 1

fn process_files_in_parallel(filenames: Vec<String>) -> io::Result<()> {
    // Divide the work into several chunks.
    const NTHREADS: usize = 8;
    let worklists = split_vec_into_chunks(filenames, NTHREADS);

    // Fork: Spawn a thread to handle each chunk.
    let mut thread_handles = vec![];
    for worklist in worklists {
        thread_handles.push(
            thread::spawn(move || process_files(worklist))
        );
    }

    // Join: Wait for all threads to finish.
    for handle in thread_handles {
        handle.join().unwrap()?;
    }

    Ok(())
}

Footnotes

  1. Programming Rust, 2nd Edition. Accessed April 5, 2022. https://learning.oreilly.com/library/view/programming-rust-2nd/9781492052586/. โ†ฉ