threads_pool

Crates.iothreads_pool
lib.rsthreads_pool
version0.2.6
sourcesrc
created_at2018-03-22 01:58:42.251114
updated_at2020-07-11 23:55:53.422342
descriptionThis package provides an easy way to create and manage thread pools, so you don't have to.
homepage
repositoryhttps://github.com/Chopinsky/thread_pool.git
max_upload_size
id56829
size139,945
Jacob Zuo (Chopinsky)

documentation

https://docs.rs/threads_pool/

README

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();
}
Commit count: 96

cargo fmt