Struct asyncio::IoServiceWork [] [src]

pub struct IoServiceWork { /* fields omitted */ }

The class to delaying until the stop of IoService is dropped.

Examples

When dropped the IoServiceWork, to stop the IoService:

use asyncio::IoService;
use std::sync::atomic::{Ordering, AtomicUsize, ATOMIC_USIZE_INIT};

static COUNT: AtomicUsize = ATOMIC_USIZE_INIT;

let io = &IoService::new();
let mut work = Some(IoService::work(io));

fn count_if_not_stopped(io: &IoService) {
  if !io.stopped() {
    COUNT.fetch_add(1, Ordering::Relaxed);
  }
}
io.post(count_if_not_stopped);
io.post(move |_| work = None);  // call IoService::stop()
io.post(count_if_not_stopped);
io.run();

assert_eq!(COUNT.load(Ordering::Relaxed), 1);

Examples

A multithreading example code:

use asyncio::IoService;
use std::thread;
use std::sync::atomic::{Ordering, AtomicUsize, ATOMIC_USIZE_INIT};

static COUNT: AtomicUsize = ATOMIC_USIZE_INIT;

let io = &IoService::new();
let _work = IoService::work(io);

let mut thrds = Vec::new();
for _ in 0..10 {
  let io = io.clone();
  thrds.push(thread::spawn(move || io.run()));
}

for _ in 0..100 {
  io.post(move |io| {
    if COUNT.fetch_add(1, Ordering::SeqCst) == 99 {
      io.stop();
    }
  });
}

io.run();
for thrd in thrds {
  thrd.join().unwrap();
}

assert_eq!(COUNT.load(Ordering::Relaxed), 100);

Trait Implementations

impl IoObject for IoServiceWork
[src]

Returns a IoService associated with this object.

impl Drop for IoServiceWork
[src]

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