#pool #threads #multithreading #thread #cargo #to

threads_pool

This package provides an easy way to create and manage thread pools, so you don’t have to

20 releases

✓ Uses Rust 2018 edition

0.2.2 Aug 5, 2019
0.2.1 Jul 14, 2019
0.1.19 Jun 28, 2019
0.1.17 Mar 6, 2019
0.1.2 Mar 28, 2018
Download history 18/week @ 2019-04-19 4/week @ 2019-04-26 25/week @ 2019-05-03 63/week @ 2019-05-10 57/week @ 2019-05-17 36/week @ 2019-05-24 32/week @ 2019-05-31 79/week @ 2019-06-07 3/week @ 2019-06-14 303/week @ 2019-06-21 37/week @ 2019-06-28 102/week @ 2019-07-05 44/week @ 2019-07-12 12/week @ 2019-07-19 20/week @ 2019-07-26

265 downloads per month
Used in 1 crate

MIT license

94KB
2K SLoC

Threads Pool

This package provides an easy way to create and manage thread pools, so you don't have to.

How to use

In your project's Cargo.toml, add dependency:

[dependencies]
threads_pool = "^0.2.0"

Then in your code:

extern crate threads_pool;

use std::time::Duration;
use std::thread::sleep;
use threads_pool::*;

fn main() {
    // The pool lives as long as the `pool` variable, when pool goes out of 
    // the scope, the thread pool will be destroyed gracefully -- all threads 
    // will finish their current job and then garnered.   
    let pool = ThreadPool::new(8);
    
    for num in 0..100 {
        pool.execute(move || {
            // Your code here...
            println!("I'm in with: {}", num);
            sleep(Duration::from_millis(10));    
        });
    }
}

The package also provide a static pool which you can create once, and use it everywhere in your application. This is called the shared_mode:

extern crate threads_pool;

use std::time::Duration;
use std::thread::sleep;
use threads_pool::shared_mode;

fn main() {
    // Create the pool here, then you can use the pool everywhere. If run a task without 
    // creating the pool, it will be equivalent of calling 'thread::spawn' on the task.
    shared_mode::initialize(8);

    for num in 0..100 {
        shared_mode::run(move || {
            // Your code here...
            println!("I'm in with: {}", num);
            sleep(Duration::from_millis(10));
        });
    }

    // The static pool must be closed, or unfinished threads will be destroyed prematurely and could cause panic in the
    // running threads. this is different from the managed pool where it can know when to shutdown as the allocated pool
    // object goes out of the scope.
    shared_mode::close();
}

Dependencies

~1.5MB
~28K SLoC