ThreadPoolBuilder

Struct ThreadPoolBuilder 

Source
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

Source

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}
Source

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}
Source

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}
Source

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}
Source

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§

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.