Struct asio::IoService [] [src]

pub struct IoService(_);

Methods

impl IoService
[src]

fn new() -> IoService

Constructs a new IoService.

Panics

Panics if too many open files.

Examples

use asio::IoService;

let io = IoService::new();

fn stop(&self)

Sets a stop request and cancel all of the waiting event in an IoService.

Examples

use asio::IoService;

let io = IoService::new();
io.stop();

fn stopped(&self) -> bool

Returns true if this has been stopped.

Examples

use asio::IoService;

let io = IoService::new();
assert_eq!(io.stopped(), false);
io.stop();
assert_eq!(io.stopped(), true);

fn reset(&self)

Resets a stopped IoService.

Examples

use asio::IoService;

let io = IoService::new();
assert_eq!(io.stopped(), false);
io.stop();
assert_eq!(io.stopped(), true);
io.reset();
assert_eq!(io.stopped(), false);

fn post<F>(&self, handler: F) where F: FnOnce(&IoService) + Send + 'static

Requests a process to invoke the given handler and return immediately.

Examples

use asio::IoService;
use std::sync::atomic::*;

let io = IoService::new();
static PASS: AtomicBool = ATOMIC_BOOL_INIT;

io.post(|_| PASS.store(true, Ordering::Relaxed));
assert_eq!(PASS.load(Ordering::Relaxed), false);

io.run();
assert_eq!(PASS.load(Ordering::Relaxed), true);

fn run(&self)

Runs all given handlers.

Examples

use asio::IoService;

let io = IoService::new();
io.run();

fn work<F: FnOnce(&IoService)>(&self, callback: F)

Runs all given handlers until call the stop().

This is ensured to not exit until explicity stopped, so it can invoking given handlers in multi-threads.

Examples

Execute 5 parallel's event loop (4 thread::spawn + 1 main thread).

use asio::IoService;
use std::thread;

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

    io.post(move |io| {
        io.stop();  // If does not explicity stop, not returns in this `work()`.
    });
});

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

Trait Implementations

impl IoObject for IoService
[src]

fn io_service(&self) -> &IoService

Returns a IoService associated with this object.

impl Debug for IoService
[src]

fn fmt(&self, f: &mut Formatter) -> Result

Formats the value using the given formatter.

impl Clone for IoService
[src]

fn clone(&self) -> IoService

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)
1.0.0

Performs copy-assignment from source. Read more