pub struct ThreadPool { /* private fields */ }Implementations§
Source§impl ThreadPool
impl ThreadPool
Sourcepub fn new(size: usize) -> ThreadPool
pub fn new(size: usize) -> ThreadPool
Create a fix size thread pool with the Polic ExceedLimitPolicy::Wait
§Example
use threadpool_executor::ThreadPool;
let pool = ThreadPool::new(1);
pool.execute(|| {println!("hello, world!");});Sourcepub fn execute<F, T>(&self, f: F) -> Result<Expectation<T>, ExecutorError>
pub fn execute<F, T>(&self, f: F) -> Result<Expectation<T>, ExecutorError>
Execute a closure in the threadpool, return a Result indicating whether the submit operation succeeded or not.
Submit operation will fail when the pool reach to the maximum_pool_size and the exeed_limit_policy is set to Reject.
You can get a Expectation<T> when Result is Ok, T here is the return type of your closure.
You can use get_result or get_result_timeout method in the Expectation object to get the result of your closure. The
two method above will block when the result is returned or timeout.
Expectation::get_result and Expectation::get_result_timeout return a Result which will return the return value of your
closure when Ok, and Err will be returned when your closure panic.
§Example
let pool = threadpool_executor::ThreadPool::new(1);
let exp = pool.execute(|| 1 + 2);
assert_eq!(exp.unwrap().get_result().unwrap(), 3);When panic:
let pool = threadpool_executor::ThreadPool::new(1);
let exp = pool.execute(|| {
panic!("panic!!!");
});
let res = exp.unwrap().get_result();
assert!(res.is_err());
if let Err(err) = res {
matches!(err.kind(), threadpool_executor::error::ErrorKind::Panic);
}