Skip to main content

ThreadpoolWork

Struct ThreadpoolWork 

Source
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

Source

pub fn new<F>( callback: F, env: Option<&mut CallbackEnviron<'_>>, ) -> Result<Self>
where F: Fn() + Send + Sync + 'static,

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.

Source

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.

Source

pub fn wait(&self)

Blocks until all queued and in-progress invocations have completed.

Source

pub fn cancel_pending(&self)

Cancels callbacks that have not yet started, then waits for any currently-executing invocations to finish.

Trait Implementations§

Source§

impl Drop for ThreadpoolWork

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for ThreadpoolWork

Source§

impl Sync for ThreadpoolWork

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.