diff options
Diffstat (limited to 'third_party/rust/tokio-threadpool/examples')
-rw-r--r-- | third_party/rust/tokio-threadpool/examples/depth.rs | 48 | ||||
-rw-r--r-- | third_party/rust/tokio-threadpool/examples/hello.rs | 24 |
2 files changed, 72 insertions, 0 deletions
diff --git a/third_party/rust/tokio-threadpool/examples/depth.rs b/third_party/rust/tokio-threadpool/examples/depth.rs new file mode 100644 index 0000000000..3d376dd38a --- /dev/null +++ b/third_party/rust/tokio-threadpool/examples/depth.rs @@ -0,0 +1,48 @@ +extern crate env_logger; +extern crate futures; +extern crate tokio_threadpool; + +use futures::future::{self, Executor}; +use tokio_threadpool::*; + +use std::sync::mpsc; + +const ITER: usize = 2_000_000; +// const ITER: usize = 30; + +fn chained_spawn() { + let pool = ThreadPool::new(); + let tx = pool.sender().clone(); + + fn spawn(tx: Sender, res_tx: mpsc::Sender<()>, n: usize) { + if n == 0 { + res_tx.send(()).unwrap(); + } else { + let tx2 = tx.clone(); + tx.execute(future::lazy(move || { + spawn(tx2, res_tx, n - 1); + Ok(()) + })) + .ok() + .unwrap(); + } + } + + loop { + println!("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); + let (res_tx, res_rx) = mpsc::channel(); + + for _ in 0..10 { + spawn(tx.clone(), res_tx.clone(), ITER); + } + + for _ in 0..10 { + res_rx.recv().unwrap(); + } + } +} + +pub fn main() { + let _ = ::env_logger::init(); + chained_spawn(); +} diff --git a/third_party/rust/tokio-threadpool/examples/hello.rs b/third_party/rust/tokio-threadpool/examples/hello.rs new file mode 100644 index 0000000000..87eb688c2d --- /dev/null +++ b/third_party/rust/tokio-threadpool/examples/hello.rs @@ -0,0 +1,24 @@ +extern crate env_logger; +extern crate futures; +extern crate tokio_threadpool; + +use futures::sync::oneshot; +use futures::*; +use tokio_threadpool::*; + +pub fn main() { + let _ = ::env_logger::init(); + + let pool = ThreadPool::new(); + let tx = pool.sender().clone(); + + let res = oneshot::spawn( + future::lazy(|| { + println!("Running on the pool"); + Ok::<_, ()>("complete") + }), + &tx, + ); + + println!("Result: {:?}", res.wait()); +} |