Struct rust_asio::IoService
[−]
[src]
pub struct IoService(_);
The core I/O process.
This is a data for all of the process of referring an IoService
.
Examples
In this example, Set 3 closures and invoke all given closures at run()
.
use asio::IoService; let io = IoService::new(); for i in 0..3 { io.post(move |_| println!("do work {}", i+1)); } io.run(); // --- Results --- // do work 1 // do work 2 // do work 3
In this example, Sets a closure in a nested closure.
use asio::IoService; let io = IoService::new(); io.post(move |io| { io.post(move |_| println!("do work 2")); println!("do work 1"); }); io.run(); // --- Results --- // do work 1 // do work 2
Methods
impl IoService
[src]
fn new() -> IoService
Make a new IoService
.
Panics
Panics if too many open file.
Examples
use asio::IoService; let io = IoService::new();
fn stop(&self)
Set 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
Determine whether a IoService
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)
Reset 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, callback: F) where F: FnOnce(&IoService) + Send + 'static
Request 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 post_strand<'a, F, T>(&self, callback: F, strand: &Strand<'a, T>) where F: FnOnce(Strand<'a, T>) + Send + 'static, T: 'static
Request a process to invoke the given handler with serialized by Strand
and return immediately.
Examples
use asio::{IoService, Strand}; let io = IoService::new(); let pass = Strand::new(&io, false); io.post_strand(|mut pass| *pass = true, &pass); assert_eq!(*pass, false); io.run(); assert_eq!(*pass, true);
fn run(&self)
fn work<F: FnOnce(&IoService)>(&self, callback: F)
Run 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 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
impl IoObject for IoService
[src]
fn io_service(&self) -> &IoService
Returns a IoService
associated with this object.