Struct ThreadPool

Source
pub struct ThreadPool { /* private fields */ }

Implementations§

Source§

impl ThreadPool

Source

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!");});
Source

pub fn execute<F, T>(&self, f: F) -> Result<Expectation<T>, ExecutorError>
where F: FnOnce() -> T + Send + UnwindSafe + 'static, T: Send + 'static,

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);
}
Source

pub fn size(&self) -> usize

Returen the current workers’ size.

Trait Implementations§

Source§

impl Drop for ThreadPool

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.