Pool

Struct Pool 

Source
pub struct Pool { /* private fields */ }
Expand description

The Pool struct

Implementations§

Source§

impl Pool

Source

pub fn new() -> Result<Self, PoolError>

Examples found in repository?
examples/without.rs (line 6)
5fn main() {
6    let pool = Pool::new().unwrap();
7    for i in 0..38 {
8        pool.push(move || test(i));
9    }
10
11    pool.join(); //wait for the pool
12}
More examples
Hide additional examples
examples/into.rs (line 12)
11fn into_inner() {
12    let pool = match Pool::new() {
13        Ok(p) => p,
14        Err(e) => e.into_inner(),
15    };
16    for i in 0..38 {
17        pool.push(move || test(i));
18    }
19
20    pool.join(); //wait for the pool
21}
22
23fn into_error() -> io::Result<()> {
24    let pool = Pool::new().map_err(|e| e.into_error())?;
25    for i in 0..38 {
26        pool.push(move || test(i));
27    }
28
29    pool.join(); //wait for the pool
30
31    Ok(())
32}
examples/scope.rs (line 6)
5fn main() {
6    let pool = Pool::new().unwrap();
7    let mut array = (0..38).into_iter().map(|i| (i, 0)).collect::<Vec<_>>();
8
9    pool.scoped(|scope| for i in array.iter_mut() {
10        scope.push(move|| i.1 = fib(i.0));
11    });
12    for (i, j) in array {
13        println!("key: {}\tvalue: {}", i, j);
14    }
15}
examples/channel.rs (line 8)
7fn main() {
8    let pool = Pool::new().unwrap();
9    let (mp, sc) = channel();
10    for i in 0..38 {
11        let mp = mp.clone();
12        pool.push(move || test(i, mp));
13    }
14
15    pool.join(); // wait for the pool
16    println!("{:?}", pool);
17
18    while let Ok((k, v)) = sc.try_recv() {
19        println!("key: {}\tvalue: {}", k, v);
20    }
21}
examples/arc_mutex.rs (line 9)
8fn main() {
9    let pool = Pool::new().unwrap();
10    // You also can use RwLock instead of Mutex if you read more than write.
11    let map = Arc::new(Mutex::new(BTreeMap::<i32, i32>::new()));
12    for i in 0..38 {
13        let map = map.clone();
14        pool.push(move || test(i, map));
15    }
16
17    pool.join(); //wait for the pool
18
19    for (k, v) in map.lock().unwrap().iter() {
20        println!("key: {}\tvalue: {}", k, v);
21    }
22}
Source

pub fn with_builder(b: Builder) -> Result<Self, PoolError>

Source

pub fn as_builder(&self) -> &Builder

Get Pool’s settings

Source

pub fn is_empty(&self) -> bool

All threads are waiting and tasks_queue’length is 0.

Source

pub fn tasks_len(&self) -> usize

Returns the length of the tasks_queue.

Source

pub fn threads_future(&self) -> usize

Contains the number of ready to create

Source

pub fn threads_alive(&self) -> usize

Returns the number of threads in the Pool.

Source

pub fn threads_waiting(&self) -> usize

Returns the number of threads that is waiting for Task in the Pool

Source

pub fn daemon_alive(&self) -> bool

The daemon thread’s status

Source

pub fn push<T>(&self, task: T)
where T: Runable + Send + 'static,

Appends a task to the Pool,

it receives Fn() + Send + 'static,FnMut() + Send + 'static and FnOnce() + Send + 'static>.

Examples found in repository?
examples/without.rs (line 8)
5fn main() {
6    let pool = Pool::new().unwrap();
7    for i in 0..38 {
8        pool.push(move || test(i));
9    }
10
11    pool.join(); //wait for the pool
12}
More examples
Hide additional examples
examples/into.rs (line 17)
11fn into_inner() {
12    let pool = match Pool::new() {
13        Ok(p) => p,
14        Err(e) => e.into_inner(),
15    };
16    for i in 0..38 {
17        pool.push(move || test(i));
18    }
19
20    pool.join(); //wait for the pool
21}
22
23fn into_error() -> io::Result<()> {
24    let pool = Pool::new().map_err(|e| e.into_error())?;
25    for i in 0..38 {
26        pool.push(move || test(i));
27    }
28
29    pool.join(); //wait for the pool
30
31    Ok(())
32}
examples/builder.rs (line 17)
5fn main() {
6    let pool = Builder::new()
7    .min(1)
8    .max(9)
9    .daemon(None) // Close
10    .timeout(None) //Close
11    .name("Worker")
12    .stack_size(1024*1024*2) //2Mib
13    .build()
14    .unwrap();
15
16    for i in 0..38 {
17        pool.push(move || test(i));
18    }
19
20    pool.join(); //wait for the pool
21    println!("{:?}", pool);
22}
examples/channel.rs (line 12)
7fn main() {
8    let pool = Pool::new().unwrap();
9    let (mp, sc) = channel();
10    for i in 0..38 {
11        let mp = mp.clone();
12        pool.push(move || test(i, mp));
13    }
14
15    pool.join(); // wait for the pool
16    println!("{:?}", pool);
17
18    while let Ok((k, v)) = sc.try_recv() {
19        println!("key: {}\tvalue: {}", k, v);
20    }
21}
examples/arc_mutex.rs (line 14)
8fn main() {
9    let pool = Pool::new().unwrap();
10    // You also can use RwLock instead of Mutex if you read more than write.
11    let map = Arc::new(Mutex::new(BTreeMap::<i32, i32>::new()));
12    for i in 0..38 {
13        let map = map.clone();
14        pool.push(move || test(i, map));
15    }
16
17    pool.join(); //wait for the pool
18
19    for (k, v) in map.lock().unwrap().iter() {
20        println!("key: {}\tvalue: {}", k, v);
21    }
22}
Source

pub fn add_threads(&self, add_num: usize) -> Result<(), (usize, Error)>

Manually add the number of threads to Pool

Source

pub fn join(&self)

wait for the pool

Examples found in repository?
examples/without.rs (line 11)
5fn main() {
6    let pool = Pool::new().unwrap();
7    for i in 0..38 {
8        pool.push(move || test(i));
9    }
10
11    pool.join(); //wait for the pool
12}
More examples
Hide additional examples
examples/into.rs (line 20)
11fn into_inner() {
12    let pool = match Pool::new() {
13        Ok(p) => p,
14        Err(e) => e.into_inner(),
15    };
16    for i in 0..38 {
17        pool.push(move || test(i));
18    }
19
20    pool.join(); //wait for the pool
21}
22
23fn into_error() -> io::Result<()> {
24    let pool = Pool::new().map_err(|e| e.into_error())?;
25    for i in 0..38 {
26        pool.push(move || test(i));
27    }
28
29    pool.join(); //wait for the pool
30
31    Ok(())
32}
examples/builder.rs (line 20)
5fn main() {
6    let pool = Builder::new()
7    .min(1)
8    .max(9)
9    .daemon(None) // Close
10    .timeout(None) //Close
11    .name("Worker")
12    .stack_size(1024*1024*2) //2Mib
13    .build()
14    .unwrap();
15
16    for i in 0..38 {
17        pool.push(move || test(i));
18    }
19
20    pool.join(); //wait for the pool
21    println!("{:?}", pool);
22}
examples/channel.rs (line 15)
7fn main() {
8    let pool = Pool::new().unwrap();
9    let (mp, sc) = channel();
10    for i in 0..38 {
11        let mp = mp.clone();
12        pool.push(move || test(i, mp));
13    }
14
15    pool.join(); // wait for the pool
16    println!("{:?}", pool);
17
18    while let Ok((k, v)) = sc.try_recv() {
19        println!("key: {}\tvalue: {}", k, v);
20    }
21}
examples/arc_mutex.rs (line 17)
8fn main() {
9    let pool = Pool::new().unwrap();
10    // You also can use RwLock instead of Mutex if you read more than write.
11    let map = Arc::new(Mutex::new(BTreeMap::<i32, i32>::new()));
12    for i in 0..38 {
13        let map = map.clone();
14        pool.push(move || test(i, map));
15    }
16
17    pool.join(); //wait for the pool
18
19    for (k, v) in map.lock().unwrap().iter() {
20        println!("key: {}\tvalue: {}", k, v);
21    }
22}
Source§

impl Pool

Source

pub fn scoped<'pool, 'scope, Scheduler>(&'pool self, scheduler: Scheduler)
where Scheduler: FnOnce(&Scoped<'pool, 'scope>),

Examples found in repository?
examples/scope.rs (lines 9-11)
5fn main() {
6    let pool = Pool::new().unwrap();
7    let mut array = (0..38).into_iter().map(|i| (i, 0)).collect::<Vec<_>>();
8
9    pool.scoped(|scope| for i in array.iter_mut() {
10        scope.push(move|| i.1 = fib(i.0));
11    });
12    for (i, j) in array {
13        println!("key: {}\tvalue: {}", i, j);
14    }
15}

Trait Implementations§

Source§

impl Debug for Pool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Pool

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for Pool

Source§

impl Sync for Pool

Auto Trait Implementations§

§

impl Freeze for Pool

§

impl !RefUnwindSafe for Pool

§

impl Unpin for Pool

§

impl !UnwindSafe for Pool

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.
Source§

impl<T> Erased for T