pub struct WorkPool<T> { /* private fields */ }Implementations§
Source§impl<T: Clone + Send> WorkPool<T>
impl<T: Clone + Send> WorkPool<T>
Sourcepub fn new(
num_threads: usize,
buf_len: Option<usize>,
) -> Result<WorkPool<T>, ()>
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
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}Sourcepub fn dispatch(&mut self, work: T)
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
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}Sourcepub fn dispatch_many(&mut self, work: Vec<T>)
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}Sourcepub fn set_executor_and_start<F>(&mut self, executor: F)
pub fn set_executor_and_start<F>(&mut self, executor: F)
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
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}Sourcepub fn close(&mut self)
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more