[−][src]Struct safina_threadpool::ThreadPool
A collection of threads and a queue for jobs (FnOnce structs) they execute.
Threads die 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.
Example
let pool = safina_threadpool::ThreadPool::new("worker", 2); 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)); 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
impl ThreadPool[src]
#[must_use]pub fn new(name: &'static str, size: usize) -> Self[src]
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".
Panics if name is empty or size is zero.
After the ThreadPool struct drops, the threads continue processing
jobs and stop when the queue is empty.
#[must_use]pub fn size(&self) -> usize[src]
Returns the number of threads in the pool.
#[must_use]pub fn num_live_threads(&self) -> usize[src]
Returns the number of threads currently alive.
pub fn schedule(&self, f: impl FnOnce() + Send + 'static)[src]
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.
See try_schedule.
Recreates any threads that panicked.
Puts f in a Box before
adding it to the queue.
pub fn try_schedule(
&self,
f: impl FnOnce() + Send + 'static
) -> Result<(), QueueFull>[src]
&self,
f: impl FnOnce() + Send + 'static
) -> Result<(), QueueFull>
Auto Trait Implementations
impl RefUnwindSafe for ThreadPool[src]
impl Send for ThreadPool[src]
impl Sync for ThreadPool[src]
impl Unpin for ThreadPool[src]
impl UnwindSafe for ThreadPool[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized, [src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized, [src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized, [src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T[src]
impl<T> From<T> for T[src]
impl<T, U> Into<U> for T where
U: From<T>, [src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>, [src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>, [src]
U: TryFrom<T>,