basic/basic.rs
1use std::thread;
2use work_pool::WorkPool;
3
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}