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 = threadpool::Builder::new()
    .num_threads(8)
    .thread_stack_size(8_000_000)
    .build();

Implementations

Initiate a new Builder.

Examples
let builder = threadpool::Builder::new();

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 = threadpool::Builder::new()
    .num_threads(8)
    .build();

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

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 = threadpool::Builder::new()
    .thread_name("foo".into())
    .build();

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

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 = 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!");
    })
}

Finalize the Builder and build the ThreadPool.

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

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.