Struct poolite::Pool
[−]
[src]
pub struct Pool { /* fields omitted */ }The Pool struct.
Methods
impl Pool[src]
Creating and Settings
The memory loading depends your task's costing,
get_min()(idle time) and get_max()(when received large number tasks).
fn new() -> Self
Creates and returns a Pool.
fn num_cpus() -> usize
Returns the number of CPUs of the current machine.
You can use it on min() ,max() or load_limit().
Maybe you also need std::usize::MIN or std::usize::MAX.
fn daemon(self, daemon: Option<u64>) -> Self
Sets whether to open the daemon for the Pool, the default is Some(5000)(thread's default idle time(ms)).
You can use None to close.
Warning: If you tasks maybe panic,please don't close it.
fn get_daemon(&self) -> Option<Duration>
Returns the value of daemon()(Option<Duration>).
fn min(self, min: usize) -> Self
Sets the minimum number of threads in the Pool,default is num_cpus()+1.
fn get_min(&self) -> usize
Returns the value of the minimum number of threads in the Pool.
fn max(self, max: usize) -> Self
Sets the maximum number of threads in the Pool,default is std::usize::MAX.
Warning: even if get_min()>get_max(),the run() method still working well.
fn get_max(&self) -> usize
Returns the value of the maximum number of threads in the Pool.
fn time_out(self, time_out: u64) -> Self
Sets thread's idle time(ms) except minimum number of threads,default is 5000(ms).
fn get_time_out(&self) -> Duration
Returns the value of the thread's idle time(Duration).
fn name<T: Into<String>>(self, name: T) -> Self where T: Debug
Sets thread's name where them in the Pool,default is None('<unnamed>').
fn get_name(&self) -> Option<String>
Returns thread's name.
fn stack_size(self, size: usize) -> Self
Sets thread's stack_size where them in the Pool,default depends on OS.
fn get_stack_size(&self) -> Option<usize>
Returns thread's stack_size.
fn load_limit(self, load_limit: usize) -> Self
Sets the value of load_limit for the Pool,
The pool will create new thread while strong_count() == 0 or tasks_queue_len()/strong_count() bigger than it,
default is num_cpus() * num_cpus().
fn get_load_limit(&self) -> usize
Returns the value of load_limit.
Complete Example for Creating and Settings:
extern crate poolite;
use poolite::Pool;
fn main() {
let pool = Pool::new()
.daemon(Some(5000))
.min(Pool::num_cpus() + 1)
.max(std::usize::MAX)
.time_out(5000) //5000ms
.name("name")
.stack_size(2 * 1024 * 1024) //2MiB
.load_limit(Pool::num_cpus() * Pool::num_cpus())
.run()
.unwrap();
}
impl Pool[src]
fn run(self) -> Result<Self, PoolError>
Lets the Pool to start running:
Add the number of min threads to the pool.
Add the daemon thread for the pool(if dont't close it).
returns
Err<PoolError>if the pool spawning the daemon thread fails.So if you close the daemon,
unwrap()is safe.You maybe need
IntoPoolorIntoIOResult(trait).extern crate poolite; use poolite::{Pool, IntoPool, IntoIOResult}; fn fun() -> std::io::Result<()> { let pool = Pool::new().run().into_pool(); let pool_io_rst = Pool::new().run().into_iorst()?; let pool_nodaemon = Pool::new().daemon(None).unwrap(); Ok(()) }
fn push<Task: IntoTask>(&self, task: Task)
Appends a task to the Pool,
it receives Fn() + Send + 'static,FnMut() + Send + 'static and
FnOnce() + Send + 'static>.
fn spawn(&self, task: Box<FnBox() + Send + 'static>)
: You should use push instead
it receives Box<Fn() + Send + 'static>,Box<FnMut() + Send + 'static> and
Box<FnOnce() + Send + 'static>(Box<FnBox() + Send + 'static>).
fn add_threads(&self, num: usize)
Manually add threads(Do not need to use it generally).
impl Pool[src]
fn is_empty(&self) -> bool
All threads are waiting and tasks_queue'length is 0.
fn join(&self)
Use it to wait for the Pool,you can also group is_empty() by yourself.
pub fn join(&self) {
self.join_ms(10); //wait for the pool 10ms
}
fn join_ms(&self, time: u64)
pub fn join_ms(&self, time: u64) {
while !self.is_empty() {
thread::sleep(Duration::from_millis(time)); //wait for the pool time(ms).
}
}
fn tasks_len(&self) -> usize
Returns the length of the tasks_queue.
Warning: tasks_len() will get the lock, please do not abuse it(Affecting performance).
fn strong_count(&self) -> usize
Approximately equal to len().
fn len(&self) -> usize
Returns the thread'number in the Pool.
fn wait_len(&self) -> usize
Returns the thread'number that is waiting in the Pool