#worker #thread-pool #jobs #sync #idle #synchronized #submission

nightly sync-threadpool

A synchronized threadpool allowing submission of new jobs only when a worker is idle

2 releases

Uses old Rust 2015

0.0.2 Jul 9, 2018
0.0.1 Jul 9, 2018

#5 in #synchronized

AGPL-3.0

15KB
122 lines

A synchronized threadpool. This threadpool allows submitting new jobs only if a worker is currently idle. This is most useful, for search-like tasks: Imagine you need to find some value in a multithreaded manner. Once the value is found, of course you don't want to submit more search jobs but probably want to use the workers for something else.

Examples

This example demonstrates finding square-roots by brute forcing. The roots are found by sending search-ranges to the threadpool.

use sync_threadpool::ThreadPool;
use std::sync::mpsc::channel;

let n_workers = 4;

const TARGET_SQUARE: u64 = 1 << 50;

let mut pool = ThreadPool::new(n_workers);

// channel to send back results
let (tx, rx) = channel();

for start in 0..0xffff_ffff_ffff {
    if let Ok(result) = rx.try_recv() {
        println!("Result found: {0:x}*{0:x} = {1:x}", result, TARGET_SQUARE);
        break;
    }
    let start = start << 16;
    let end = start + 0xffff;
    let range = start..end;
    let tx = tx.clone();
    let job = move || {
        for i in range {
            if i*i == TARGET_SQUARE {
                tx.send(i).unwrap();
            }
        }
    };

    pool.execute(job);
}

No runtime deps