summaryrefslogtreecommitdiffstats
path: root/third_party/rust/tokio-threadpool/examples/depth.rs
blob: 3d376dd38adc9e3d60e191658d0bb95bc1cc95bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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();
}