Struct safina_threadpool::ThreadPool
source · [−]pub struct ThreadPool { /* private fields */ }
Expand description
A collection of threads and a queue for jobs (FnOnce
structs) they execute.
Threads stop when they execute a job that panics.
If one thread survives, it will recreate all the threads.
The next call to schedule
or try_schedule
also recreates threads.
If your threadpool load is bursty and you want to automatically recover
from an all-threads-panicked state, you could use
safina_timer
to periodically call
schedule
or try_schedule
.
After drop, threads stop as they become idle.
Example
let pool =
safina_threadpool::ThreadPool::new("worker", 2).unwrap();
let receiver = {
let (sender, receiver) =
std::sync::mpsc::channel();
for data in data_source {
let sender_clone = sender.clone();
pool.schedule(
move || process_data(data, sender_clone));
}
receiver
};
let results: Vec<ProcessResult> =
receiver.iter().collect();
// ...
let pool =
Arc::new(
safina_threadpool::ThreadPool::new("worker", 2).unwrap());
let executor = safina_executor::Executor::default();
safina_timer::start_timer_thread();
let pool_clone = pool.clone();
executor.spawn(async move {
loop {
safina_timer::sleep_for(Duration::from_millis(500)).await;
pool_clone.schedule(|| {});
}
});
Implementations
sourceimpl ThreadPool
impl ThreadPool
sourcepub fn new(name: &'static str, size: usize) -> Result<Self, NewThreadPoolError>
pub fn new(name: &'static str, size: usize) -> Result<Self, NewThreadPoolError>
Creates a new thread pool containing size
threads.
The threads all start immediately.
Threads are named with name
with a number.
For example, ThreadPool::new("worker", 2)
creates threads named “worker-1” and “worker-2”.
If one of those threads panics, the pool creates “worker-3”.
After the ThreadPool
struct drops, the threads continue processing
jobs and stop when the queue is empty.
Errors
Returns an error when name
is empty, size
is zero, or it fails to start the threads.
sourcepub fn num_live_threads(&self) -> usize
pub fn num_live_threads(&self) -> usize
Returns the number of threads currently alive.
sourcepub fn schedule<F: FnOnce() + Send + 'static>(&self, f: F)
pub fn schedule<F: FnOnce() + Send + 'static>(&self, f: F)
Adds a job to the queue. The next idle thread will execute it. Jobs are started in FIFO order.
Blocks when the queue is full or no threads are running.
See try_schedule
.
Recreates any threads that panicked. Retries on failure to start a new thread.
Puts f
in a Box
before
adding it to the queue.
sourcepub fn try_schedule(
&self,
f: impl FnOnce() + Send + 'static
) -> Result<(), TryScheduleError>
pub fn try_schedule(
&self,
f: impl FnOnce() + Send + 'static
) -> Result<(), TryScheduleError>
Adds a job to the queue and then starts threads to replace any panicked threads. The next idle thread will execute the job. Starts jobs in FIFO order.
Puts f
in a Box
before
adding it to the queue.
Errors
Returns an error when the queue is full or it fails to start a thread.
If the return value is not TryScheduleError::QueueFull
then it added the job to the queue.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for ThreadPool
impl Send for ThreadPool
impl Sync for ThreadPool
impl Unpin for ThreadPool
impl UnwindSafe for ThreadPool
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more