Struct asyncio::IoContextWork [] [src]

pub struct IoContextWork(_);

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

impl Drop for IoContextWork
[src]

A method called when the value goes out of scope. Read more

impl AsIoContext for IoContextWork
[src]