ThreadPool

Struct ThreadPool 

Source
pub struct ThreadPool { /* private fields */ }
Expand description

A NUMA aware thread pool

Implementations§

Source§

impl ThreadPool

Source

pub fn shutdown(&mut self)

Wait for all threads to finish and remove them from the pool

Source

pub fn schedule_task<F>(&self, task: F)
where F: FnOnce() + Send + Sync + 'static,

Run a function on an available thread

Examples found in repository?
examples/thread_pool.rs (line 22)
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 get_cpus() -> Vec<CPUNode>

Returns all CPUs and their associated NUMA nodes

Examples found in repository?
examples/thread_pool.rs (line 8)
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}
32
33fn print_task_info(task_id: u16) {
34    println!("Task {} executed in thread - {:?}", task_id, std::thread::current().id());
35    let thread_cpus = ThreadPool::get_cpus();
36
37    println!("CPU count available to task {}: {}", task_id, thread_cpus.len());
38    std::thread::sleep(std::time::Duration::from_millis(1000));
39}

Trait Implementations§

Source§

impl Drop for ThreadPool

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for ThreadPool

Source§

impl Sync for ThreadPool

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.