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 wait(&self)
pub fn wait(&self)
Blocks until all queued and in-progress invocations have completed.
This type has no separately-named synchronous close: this is the drain
that Drop would otherwise perform, so calling it discharges the
obligation Drop reports.
Sourcepub fn stop_and_drain(&self)
pub fn stop_and_drain(&self)
Stop accepting work and block until none is queued or executing.
The drain every type in this crate offers under this name, so a caller tearing down a mixed set of objects can reach for one method.
There is nothing to stop on a work object – a submission cannot be
withdrawn, only waited for – so this is exactly wait,
which is why that method is documented as this type’s drain. The name
exists because a caller should not have to know which of this crate’s
types has something to stop.
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.
Settles the drain obligation, exactly as wait does and
for the same reason: on return nothing is queued and nothing is
executing, which is what the obligation tracks. It used not to, so a
caller who submitted, cancelled, and dropped was told at drop that it
still owed a drain – and under fail-fast that report is a panic, on a
caller that had done nothing wrong.
Sound here, and deliberately not done on the wait or the timer.
Those two can be re-armed from inside their own callbacks
(crate::wait::WaitActivation::rearm,
crate::timer::TimerFiring::rearm_after), so a callback running
during the cancel can leave the object live again before this returns –
which is why their stop_and_drain suppresses re-arms before draining
and only then settles. A work callback is a bare Fn() with no handle
to its object, so nothing can re-queue it except an explicit
submit, and that arms the obligation again itself.