[][src]Struct threader::thread_pool::ThreadPool

pub struct ThreadPool { /* fields omitted */ }

An executor which distributes tasks across multiple threads using a work-stealing scheduler. Tasks can be spawned on it by calling the spawn method on the ThreadPool. Note that since this executor moves futures between different threads, the future in question must be Send.

Examples

use std::io;
use threader::{
    executor::Executor,
    thread_pool::ThreadPool,
    net::tcp::TcpStream,
};

fn main() -> io::Result<()> {
    let mut pool = ThreadPool::new()?;
    let addr = "10.0.0.1:80".parse().unwrap();

    pool.spawn(async move {
        let _stream = TcpStream::connect(&addr);
    });

    pool.shutdown_on_idle();
    Ok(())
}

Methods

impl ThreadPool[src]

pub fn new() -> Result<ThreadPool>[src]

Creates a new ThreadPool instance with a number of threads equal to the number of logical CPU cores in a given machine. Returns any errors that may have occurred in creating the thread pool.

Examples

use std::io;
use threader::thread_pool::ThreadPool;

fn main() -> io::Result<()> {
    let pool = ThreadPool::new()?;
    Ok(())
}

pub fn with_threads(count: usize) -> Result<ThreadPool>[src]

Creates a new ThreadPool instance with a number of threads equal to count. count must not be zero, or this method will panic. Like ThreadPool::new, this method returns any errors that occurred when creating the thread pool.

Panics

Panics if count is equal to zero.

Examples

use std::io;
use threader::thread_pool::ThreadPool;

fn main() -> io::Result<()> {
    let pool = ThreadPool::with_threads(1)?;
    Ok(())
}

pub fn shutdown_on_idle(&mut self)[src]

Shuts down the ThreadPool when all worker threads are idle. This method blocks the current thread until all of the worker threads have been joined.

pub fn shutdown_now(&mut self)[src]

Shuts down the ThreadPool immediately, sending a message to all worker threads to shut down. This emethod blocks the current thread until all worker threads have been joined, but this blocking shouldn't be noticeable.

Trait Implementations

impl<F> Executor<F> for ThreadPool where
    F: Future<Output = ()> + Send + 'static, 
[src]

impl Drop for ThreadPool[src]

Auto Trait Implementations

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]