A lite thread pool library written for Rust.
Usage
Cargo.toml
[]
= "0.3.0"
or
[]
= { = "https://github.com/biluohc/poolite", = "master", = "0.3.0" }
Explain
Create a thread Pool:
- use
poolite::Pool::new()create a thread_pool.
The following are optional:
min()receiveusizeas minimum number of threads in pool,default is cpu's number+1.time_out()receiveu64as thread's idle time(ms) except minimum number of threads,default is 5000(ms).name()receiveAsRef<str>as thread's name,default is None.stack_size()receiveusizeas thread's stack_size,default depends on OS.load_limit()receiveusizeas the load_limit, pool will create new thread whiletasks_queue_len()/threadsbigger than it,default is cpu's number.
Ps:Pool will always block whenmin()is 0 andload_limit()is'not 0,untiltasks_queue_len()/threadsbigger than load_limit.
Let Pool to start run:
run()let pool to start run(Add number of min threads to the pool).
Add a task to the Pool:
spawn()receiveBox<Fn() + Send + 'static>,Box<FnMut() + Send + 'static>andBox<FnOnce() + Send + 'static>(Box<FnBox() + Send + 'static>).
Get Pool's status
len()return a usize of the thread'number in pool.wait_len()return a usize of the thread'number that is waiting in pooltasks_len()return a usize of the length of the tasks_queue.is_empty()return a bool, all threads are waiting and tasks_queue'length is 0.
Drop
- while leave scope,pool will drop automatically.
Example
extern crate poolite;
use BTreeMap;
use ;
use Duration;
use thread;
ChangLog
- 2017-0112 0.3.0 remove all
unwrap()and addload_limit(),is_empty(), tasks_len(), len(), wait_len(), strong_count()methods. - 2016-0102 0.2.1 use unstable
FnBox()to supportFnOnce()(Only support Nightly now,Stable or Beta should use 0.2.0). - 2016-0101 0.2.0 add
min(),time_out(),name(),stack_size(),run()methods.