pub struct Builder { /* private fields */ }
Expand description

ThreadPool factory, which can be used in order to configure the properties of the ThreadPool.

The three configuration options available:

  • num_threads: maximum number of threads that will be alive at any given moment by the built ThreadPool
  • thread_name: thread name for each of the threads spawned by the built ThreadPool
  • thread_stack_size: stack size (in bytes) for each of the threads spawned by the built ThreadPool

Examples

Build a ThreadPool that uses a maximum of eight threads simultaneously and each thread has a 8 MB stack size:

let pool = blocking_threadpool::Builder::new()
    .num_threads(8)
    .thread_stack_size(8_000_000)
    .build();

Implementations§

source§

impl Builder

source

pub fn new() -> Builder

Initiate a new Builder.

Examples
let builder = blocking_threadpool::Builder::new();
source

pub fn num_threads(self, num_threads: usize) -> Builder

Set the maximum number of worker-threads that will be alive at any given moment by the built ThreadPool. If not specified, defaults the number of threads to the number of CPUs.

Panics

This method will panic if num_threads is 0.

Examples

No more than eight threads will be alive simultaneously for this pool:

use std::thread;

let pool = blocking_threadpool::Builder::new()
    .num_threads(8)
    .build();

for _ in 0..100 {
    pool.execute(|| {
        println!("Hello from a worker thread!")
    })
}
source

pub fn queue_len(self, len: usize) -> Builder

Set the maximum number of pending jobs that can be queued to the ThreadPool. Once the queue is full further calls will block until slots become available. A len of 0 will always block until a thread is available. If not specified, defaults to unlimited.

Panics

This method will panic if len is less-than 0;

Examples

With a single thread and a queue len of 1, the final execute will have to wait until the first job finishes to be queued.

use std::thread;
use std::time::Duration;

let pool = blocking_threadpool::Builder::new()
    .num_threads(1)
    .queue_len(1)
    .build();

for _ in 0..2 {
    pool.execute(|| {
        println!("Hello from a worker thread! I'm going to rest now...");
        thread::sleep(Duration::from_secs(10));
        println!("All done!");
    })
}

pool.execute(|| {
  println!("Hello from 10 seconds in the future!");
});
source

pub fn thread_name(self, name: String) -> Builder

Set the thread name for each of the threads spawned by the built ThreadPool. If not specified, threads spawned by the thread pool will be unnamed.

Examples

Each thread spawned by this pool will have the name “foo”:

use std::thread;

let pool = blocking_threadpool::Builder::new()
    .thread_name("foo".into())
    .build();

for _ in 0..100 {
    pool.execute(|| {
        assert_eq!(thread::current().name(), Some("foo"));
    })
}
source

pub fn thread_stack_size(self, size: usize) -> Builder

Set the stack size (in bytes) for each of the threads spawned by the built ThreadPool. If not specified, threads spawned by the threadpool will have a stack size as specified in the std::thread documentation.

Examples

Each thread spawned by this pool will have a 4 MB stack:

let pool = blocking_threadpool::Builder::new()
    .thread_stack_size(4_000_000)
    .build();

for _ in 0..100 {
    pool.execute(|| {
        println!("This thread has a 4 MB stack size!");
    })
}
source

pub fn build(self) -> ThreadPool

Finalize the Builder and build the ThreadPool.

Examples
let pool = blocking_threadpool::Builder::new()
    .num_threads(8)
    .thread_stack_size(4_000_000)
    .build();

Trait Implementations§

source§

impl Clone for Builder

source§

fn clone(&self) -> Builder

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Default for Builder

source§

fn default() -> Builder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.