pub struct IoContextWork(/* private fields */);
Expand description
The class to delaying until the stop of IoContext
is dropped.
§Examples
When dropped the IoContextWork
, to stop the IoContext
:
use asyncio::IoContext;
use std::sync::atomic::{Ordering, AtomicUsize, ATOMIC_USIZE_INIT};
static COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
let ctx = &IoContext::new().unwrap();
let mut work = Some(IoContext::work(ctx));
fn count_if_not_stopped(ctx: &IoContext) {
if !ctx.stopped() {
COUNT.fetch_add(1, Ordering::Relaxed);
}
}
ctx.post(count_if_not_stopped);
ctx.post(move |_| work = None); // call IoContext::stop()
ctx.post(count_if_not_stopped);
ctx.run();
assert_eq!(COUNT.load(Ordering::Relaxed), 1);
§Examples
A multithreading example code:
use asyncio::IoContext;
use std::thread;
use std::sync::atomic::{Ordering, AtomicUsize, ATOMIC_USIZE_INIT};
static COUNT: AtomicUsize = ATOMIC_USIZE_INIT;
let ctx = &IoContext::new().unwrap();
let _work = IoContext::work(ctx);
let mut thrds = Vec::new();
for _ in 0..10 {
let ctx = ctx.clone();
thrds.push(thread::spawn(move|| ctx.run()));
}
for _ in 0..100 {
ctx.post(move|ctx| {
if COUNT.fetch_add(1, Ordering::SeqCst) == 99 {
ctx.stop();
}
});
}
ctx.run();
for thrd in thrds {
thrd.join().unwrap();
}
assert_eq!(COUNT.load(Ordering::Relaxed), 100);
Trait Implementations§
Source§impl AsIoContext for IoContextWork
impl AsIoContext for IoContextWork
Auto Trait Implementations§
impl Freeze for IoContextWork
impl !RefUnwindSafe for IoContextWork
impl Send for IoContextWork
impl !Sync for IoContextWork
impl Unpin for IoContextWork
impl !UnwindSafe for IoContextWork
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more