pub struct ThreadPool { /* private fields */ }Expand description
sender is the Sender end of the channel used for passing tasks to the workers
workers possess threads and are responsible for running the tasks they receiver from the channels in their own threads
Each worker possesses a superficial clone of a single Receiver end that they borrow mutably through parking_lot::Mutex borrow
Implementations§
Source§impl ThreadPool
impl ThreadPool
Sourcepub fn new(size: usize) -> Self
pub fn new(size: usize) -> Self
Creates a new ThreadPool with the specified number of worker threads.
The executor service will spawn size worker threads, each of which will
process tasks submitted to the service using the execute method.
§Arguments
size- The number of worker threads to create.
§Returns
A new FixedThreadPool object.
§Panics
This function will panic if the value of size is equal to zero
§Example
use thread_runner::ThreadPool;
let executor = ThreadPool::new(4);
// execute some tasks
executor.join();Sourcepub fn execute<F: Send + 'static + FnOnce()>(&self, f: F)
pub fn execute<F: Send + 'static + FnOnce()>(&self, f: F)
Executes the given closure as a task in a worker thread.
This is achieved by sending the task to a pool of workers, who compete to execute it in their threads.
Tasks submitted through the channel are executed in the order they are received (FIFO - First In, First Out). This means if the tasks outnumber the workers, the later tasks are suspended until the earlier tasks are executed.
§Example
use thread_runner::ThreadPool;
let executor = ThreadPool::new(4);
for val in 0..10 {
executor.execute(move || println!("{}", val));
}
executor.join();§Note
If you want to wait for the submitted tasks to finish executing, you should call join on the executor service.