pub struct ThreadPoolBuilder { /* private fields */ }Expand description
use rustpool::{ThreadPool, ThreadPoolBuilder};
fn main() {
// change USE_ALL_CPUS to target multiple or a single CPU
const USE_ALL_CPUS: bool = true;
let cpu = ThreadPool::get_cpus()[0];
let builder: ThreadPoolBuilder;
if USE_ALL_CPUS {
builder = ThreadPoolBuilder::new().for_all_cpus().count(2);
} else {
builder = ThreadPoolBuilder::new().for_cpu(cpu).count(2);
}
let res = builder.build();
if res.is_ok() {
println!("Thread pool created");
let tp = res.unwrap();
tp.schedule_task(|| print_task_info(1));
tp.schedule_task(|| print_task_info(2));
// on drop, the pool will wait for all running tasks to end
return;
}
// panic if we can't create the thread pool
std::panic::panic_any(res.err());
}
fn print_task_info(task_id: u16) {
println!("Task {} executed in thread - {:?}", task_id, std::thread::current().id());
let thread_cpus = ThreadPool::get_cpus();
println!("CPU count available to task {}: {}", task_id, thread_cpus.len());
std::thread::sleep(std::time::Duration::from_millis(1000));
}Builder for thread pools
Implementations§
Source§impl ThreadPoolBuilder
impl ThreadPoolBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new thread pool builder
Examples found in repository?
examples/thread_pool.rs (line 12)
4fn main() {
5 // change USE_ALL_CPUS to target multiple or a single CPU
6 const USE_ALL_CPUS: bool = false;
7
8 let cpu = ThreadPool::get_cpus()[0];
9
10 let builder: ThreadPoolBuilder;
11 if USE_ALL_CPUS {
12 builder = ThreadPoolBuilder::new().for_all_cpus().count(2);
13 } else {
14 builder = ThreadPoolBuilder::new().for_cpu(cpu).count(2);
15 }
16 let res = builder.build();
17
18 if res.is_ok() {
19 println!("Thread pool created");
20 let tp = res.unwrap();
21
22 tp.schedule_task(|| print_task_info(1));
23 tp.schedule_task(|| print_task_info(2));
24
25 // on drop, the pool will wait for all running tasks to end
26 return;
27 }
28
29 // panic if we can't create the thread pool
30 std::panic::panic_any(res.err());
31}Sourcepub fn count(self, count: usize) -> Self
pub fn count(self, count: usize) -> Self
Sets the number of threads to be created in the pool
Examples found in repository?
examples/thread_pool.rs (line 12)
4fn main() {
5 // change USE_ALL_CPUS to target multiple or a single CPU
6 const USE_ALL_CPUS: bool = false;
7
8 let cpu = ThreadPool::get_cpus()[0];
9
10 let builder: ThreadPoolBuilder;
11 if USE_ALL_CPUS {
12 builder = ThreadPoolBuilder::new().for_all_cpus().count(2);
13 } else {
14 builder = ThreadPoolBuilder::new().for_cpu(cpu).count(2);
15 }
16 let res = builder.build();
17
18 if res.is_ok() {
19 println!("Thread pool created");
20 let tp = res.unwrap();
21
22 tp.schedule_task(|| print_task_info(1));
23 tp.schedule_task(|| print_task_info(2));
24
25 // on drop, the pool will wait for all running tasks to end
26 return;
27 }
28
29 // panic if we can't create the thread pool
30 std::panic::panic_any(res.err());
31}Sourcepub fn for_all_cpus(self) -> Self
pub fn for_all_cpus(self) -> Self
Creates a pool with threads on all available CPUs. This option cannot be used with for_cpu
Examples found in repository?
examples/thread_pool.rs (line 12)
4fn main() {
5 // change USE_ALL_CPUS to target multiple or a single CPU
6 const USE_ALL_CPUS: bool = false;
7
8 let cpu = ThreadPool::get_cpus()[0];
9
10 let builder: ThreadPoolBuilder;
11 if USE_ALL_CPUS {
12 builder = ThreadPoolBuilder::new().for_all_cpus().count(2);
13 } else {
14 builder = ThreadPoolBuilder::new().for_cpu(cpu).count(2);
15 }
16 let res = builder.build();
17
18 if res.is_ok() {
19 println!("Thread pool created");
20 let tp = res.unwrap();
21
22 tp.schedule_task(|| print_task_info(1));
23 tp.schedule_task(|| print_task_info(2));
24
25 // on drop, the pool will wait for all running tasks to end
26 return;
27 }
28
29 // panic if we can't create the thread pool
30 std::panic::panic_any(res.err());
31}Sourcepub fn for_cpu(self, cpu: CPUNode) -> Self
pub fn for_cpu(self, cpu: CPUNode) -> Self
Tells the builder to create a thread pool where all threads have affinity with a single CPU core.
This option cannot be used with for_all_cpus
Examples found in repository?
examples/thread_pool.rs (line 14)
4fn main() {
5 // change USE_ALL_CPUS to target multiple or a single CPU
6 const USE_ALL_CPUS: bool = false;
7
8 let cpu = ThreadPool::get_cpus()[0];
9
10 let builder: ThreadPoolBuilder;
11 if USE_ALL_CPUS {
12 builder = ThreadPoolBuilder::new().for_all_cpus().count(2);
13 } else {
14 builder = ThreadPoolBuilder::new().for_cpu(cpu).count(2);
15 }
16 let res = builder.build();
17
18 if res.is_ok() {
19 println!("Thread pool created");
20 let tp = res.unwrap();
21
22 tp.schedule_task(|| print_task_info(1));
23 tp.schedule_task(|| print_task_info(2));
24
25 // on drop, the pool will wait for all running tasks to end
26 return;
27 }
28
29 // panic if we can't create the thread pool
30 std::panic::panic_any(res.err());
31}Sourcepub fn build(&self) -> Result<ThreadPool, String>
pub fn build(&self) -> Result<ThreadPool, String>
Creates a thread pool using the given builder parameters
Examples found in repository?
examples/thread_pool.rs (line 16)
4fn main() {
5 // change USE_ALL_CPUS to target multiple or a single CPU
6 const USE_ALL_CPUS: bool = false;
7
8 let cpu = ThreadPool::get_cpus()[0];
9
10 let builder: ThreadPoolBuilder;
11 if USE_ALL_CPUS {
12 builder = ThreadPoolBuilder::new().for_all_cpus().count(2);
13 } else {
14 builder = ThreadPoolBuilder::new().for_cpu(cpu).count(2);
15 }
16 let res = builder.build();
17
18 if res.is_ok() {
19 println!("Thread pool created");
20 let tp = res.unwrap();
21
22 tp.schedule_task(|| print_task_info(1));
23 tp.schedule_task(|| print_task_info(2));
24
25 // on drop, the pool will wait for all running tasks to end
26 return;
27 }
28
29 // panic if we can't create the thread pool
30 std::panic::panic_any(res.err());
31}Auto Trait Implementations§
impl Freeze for ThreadPoolBuilder
impl RefUnwindSafe for ThreadPoolBuilder
impl Send for ThreadPoolBuilder
impl Sync for ThreadPoolBuilder
impl Unpin for ThreadPoolBuilder
impl UnwindSafe for ThreadPoolBuilder
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