WorkPool

Struct WorkPool 

Source
pub struct WorkPool<T> { /* private fields */ }

Implementations§

Source§

impl<T: Clone + Send> WorkPool<T>

Source

pub fn new( num_threads: usize, buf_len: Option<usize>, ) -> Result<WorkPool<T>, ()>

Create a new WorkPool

Examples found in repository?
examples/stream.rs (line 18)
11fn main() {
12    let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
13    let listener = TcpListener::bind(addr).expect("Failed to bind to port 8080");
14
15    // Setting the threads to 0 will let the system choose the number
16    // of threads. Setting the buffer length to None will default the
17    // buffer length to a reasonable amount.
18    let mut pool = WorkPool::new(0, None).expect("Failed to build work pool");
19    pool.set_executor_and_start(|stream| { handle(stream); });
20
21    println!("Bound to port 8080 - Send a request!");
22    println!("Press Ctrl+C to quit ...");
23
24    for stream in listener.incoming() {
25        match stream {
26            Ok(s) => pool.dispatch(Arc::new(s)),
27            Err(e) => eprintln!("failed to get incomign stream {:?}", e),
28        }
29    }
30}
More examples
Hide additional examples
examples/basic.rs (line 11)
4fn main() {
5    // Specify the number of threads to be used and the number of work
6    // item spaces to start with.
7    //
8    // Setting the threads to 0 will let the system choose the number
9    // of threads. Setting the buffer length to None will default the
10    // buffer length to a reasonable amount.
11    let mut pool = WorkPool::new(0, None).expect("Failed to build work pool");
12
13    pool.set_executor_and_start(|work| {
14        println!("thread {:?} got item {}", thread::current().id(), work);
15    });
16
17    // Dispatch some work to do
18    pool.dispatch(1);
19    pool.dispatch(2);
20    pool.dispatch(3);
21    pool.dispatch(4);
22
23    // Or, dispatch a bunch of work at once
24    pool.dispatch_many(vec![5, 6, 7, 8]);
25
26    // Closing the pool will send a quit message to the threads and
27    // block while it waits for the threads to join.
28    //
29    // Dropping the pool will send a quit message to
30    // the threads and detach them - you won't have to
31    // wait for the threads to join
32    pool.close();
33
34    println!("Done");
35}
Source

pub fn dispatch(&mut self, work: T)

Send a job to the pool

Examples found in repository?
examples/stream.rs (line 26)
11fn main() {
12    let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
13    let listener = TcpListener::bind(addr).expect("Failed to bind to port 8080");
14
15    // Setting the threads to 0 will let the system choose the number
16    // of threads. Setting the buffer length to None will default the
17    // buffer length to a reasonable amount.
18    let mut pool = WorkPool::new(0, None).expect("Failed to build work pool");
19    pool.set_executor_and_start(|stream| { handle(stream); });
20
21    println!("Bound to port 8080 - Send a request!");
22    println!("Press Ctrl+C to quit ...");
23
24    for stream in listener.incoming() {
25        match stream {
26            Ok(s) => pool.dispatch(Arc::new(s)),
27            Err(e) => eprintln!("failed to get incomign stream {:?}", e),
28        }
29    }
30}
More examples
Hide additional examples
examples/basic.rs (line 18)
4fn main() {
5    // Specify the number of threads to be used and the number of work
6    // item spaces to start with.
7    //
8    // Setting the threads to 0 will let the system choose the number
9    // of threads. Setting the buffer length to None will default the
10    // buffer length to a reasonable amount.
11    let mut pool = WorkPool::new(0, None).expect("Failed to build work pool");
12
13    pool.set_executor_and_start(|work| {
14        println!("thread {:?} got item {}", thread::current().id(), work);
15    });
16
17    // Dispatch some work to do
18    pool.dispatch(1);
19    pool.dispatch(2);
20    pool.dispatch(3);
21    pool.dispatch(4);
22
23    // Or, dispatch a bunch of work at once
24    pool.dispatch_many(vec![5, 6, 7, 8]);
25
26    // Closing the pool will send a quit message to the threads and
27    // block while it waits for the threads to join.
28    //
29    // Dropping the pool will send a quit message to
30    // the threads and detach them - you won't have to
31    // wait for the threads to join
32    pool.close();
33
34    println!("Done");
35}
Source

pub fn dispatch_many(&mut self, work: Vec<T>)

Send a list of jobs to the pool

Examples found in repository?
examples/basic.rs (line 24)
4fn main() {
5    // Specify the number of threads to be used and the number of work
6    // item spaces to start with.
7    //
8    // Setting the threads to 0 will let the system choose the number
9    // of threads. Setting the buffer length to None will default the
10    // buffer length to a reasonable amount.
11    let mut pool = WorkPool::new(0, None).expect("Failed to build work pool");
12
13    pool.set_executor_and_start(|work| {
14        println!("thread {:?} got item {}", thread::current().id(), work);
15    });
16
17    // Dispatch some work to do
18    pool.dispatch(1);
19    pool.dispatch(2);
20    pool.dispatch(3);
21    pool.dispatch(4);
22
23    // Or, dispatch a bunch of work at once
24    pool.dispatch_many(vec![5, 6, 7, 8]);
25
26    // Closing the pool will send a quit message to the threads and
27    // block while it waits for the threads to join.
28    //
29    // Dropping the pool will send a quit message to
30    // the threads and detach them - you won't have to
31    // wait for the threads to join
32    pool.close();
33
34    println!("Done");
35}
Source

pub fn set_executor_and_start<F>(&mut self, executor: F)
where F: FnOnce(T) + Copy + Send + 'static, T: Send + 'static,

Setup the job executor function and start threads

Examples found in repository?
examples/stream.rs (line 19)
11fn main() {
12    let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
13    let listener = TcpListener::bind(addr).expect("Failed to bind to port 8080");
14
15    // Setting the threads to 0 will let the system choose the number
16    // of threads. Setting the buffer length to None will default the
17    // buffer length to a reasonable amount.
18    let mut pool = WorkPool::new(0, None).expect("Failed to build work pool");
19    pool.set_executor_and_start(|stream| { handle(stream); });
20
21    println!("Bound to port 8080 - Send a request!");
22    println!("Press Ctrl+C to quit ...");
23
24    for stream in listener.incoming() {
25        match stream {
26            Ok(s) => pool.dispatch(Arc::new(s)),
27            Err(e) => eprintln!("failed to get incomign stream {:?}", e),
28        }
29    }
30}
More examples
Hide additional examples
examples/basic.rs (lines 13-15)
4fn main() {
5    // Specify the number of threads to be used and the number of work
6    // item spaces to start with.
7    //
8    // Setting the threads to 0 will let the system choose the number
9    // of threads. Setting the buffer length to None will default the
10    // buffer length to a reasonable amount.
11    let mut pool = WorkPool::new(0, None).expect("Failed to build work pool");
12
13    pool.set_executor_and_start(|work| {
14        println!("thread {:?} got item {}", thread::current().id(), work);
15    });
16
17    // Dispatch some work to do
18    pool.dispatch(1);
19    pool.dispatch(2);
20    pool.dispatch(3);
21    pool.dispatch(4);
22
23    // Or, dispatch a bunch of work at once
24    pool.dispatch_many(vec![5, 6, 7, 8]);
25
26    // Closing the pool will send a quit message to the threads and
27    // block while it waits for the threads to join.
28    //
29    // Dropping the pool will send a quit message to
30    // the threads and detach them - you won't have to
31    // wait for the threads to join
32    pool.close();
33
34    println!("Done");
35}
Source

pub fn close(&mut self)

Send a quit message to all threads and wait for them to join.

Examples found in repository?
examples/basic.rs (line 32)
4fn main() {
5    // Specify the number of threads to be used and the number of work
6    // item spaces to start with.
7    //
8    // Setting the threads to 0 will let the system choose the number
9    // of threads. Setting the buffer length to None will default the
10    // buffer length to a reasonable amount.
11    let mut pool = WorkPool::new(0, None).expect("Failed to build work pool");
12
13    pool.set_executor_and_start(|work| {
14        println!("thread {:?} got item {}", thread::current().id(), work);
15    });
16
17    // Dispatch some work to do
18    pool.dispatch(1);
19    pool.dispatch(2);
20    pool.dispatch(3);
21    pool.dispatch(4);
22
23    // Or, dispatch a bunch of work at once
24    pool.dispatch_many(vec![5, 6, 7, 8]);
25
26    // Closing the pool will send a quit message to the threads and
27    // block while it waits for the threads to join.
28    //
29    // Dropping the pool will send a quit message to
30    // the threads and detach them - you won't have to
31    // wait for the threads to join
32    pool.close();
33
34    println!("Done");
35}

Trait Implementations§

Source§

impl<T: Debug> Debug for WorkPool<T>

Source§

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

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

impl<T> Drop for WorkPool<T>

Source§

fn drop(&mut self)

When dropping this struct, threads will be detached

Auto Trait Implementations§

§

impl<T> Freeze for WorkPool<T>

§

impl<T> !RefUnwindSafe for WorkPool<T>

§

impl<T> Send for WorkPool<T>
where T: Send,

§

impl<T> Sync for WorkPool<T>
where T: Send,

§

impl<T> Unpin for WorkPool<T>

§

impl<T> !UnwindSafe for WorkPool<T>

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.