pub struct ThreadpoolWork { /* private fields */ }Expand description
An owned thread-pool work object.
Each call to ThreadpoolWork::submit queues one invocation of the callback
on the process thread pool. Multiple invocations may execute concurrently.
Drop calls WaitForThreadpoolWorkCallbacks (allowing in-flight callbacks
to complete) before releasing the callback context, so the captured closure
remains valid for the full lifetime of every callback execution.
§Examples
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use windows_threadpool_sys::work::ThreadpoolWork;
let total = Arc::new(AtomicUsize::new(0));
let counter = Arc::clone(&total);
let work = ThreadpoolWork::new(move || {
counter.fetch_add(1, Ordering::SeqCst);
}, None)?;
// Each submission queues one independent invocation; they may run
// concurrently, so the callback must tolerate that.
for _ in 0..8 {
work.submit();
}
work.wait();
assert_eq!(total.load(Ordering::SeqCst), 8);Implementations§
Source§impl ThreadpoolWork
impl ThreadpoolWork
Sourcepub fn new<F>(
callback: F,
env: Option<&mut CallbackEnviron<'_>>,
) -> Result<Self>
pub fn new<F>( callback: F, env: Option<&mut CallbackEnviron<'_>>, ) -> Result<Self>
Creates a new work object that invokes callback each time it is submitted.
Pass Some(env) to associate a non-default callback environment; None
uses the process-default pool with default priority.
Sourcepub fn submit(&self)
pub fn submit(&self)
Queues one invocation of the callback on the thread pool.
May be called repeatedly; each call queues an independent invocation. Multiple queued invocations may execute concurrently.
Sourcepub fn cancel_pending(&self)
pub fn cancel_pending(&self)
Cancels callbacks that have not yet started, then waits for any currently-executing invocations to finish.