summaryrefslogtreecommitdiff
path: root/examples/channel.rs
blob: 267d476a37accc4a0336b78809a8551528c0d502 (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
//! You can use async channel between threads(with `sync` feature).
//! Remember: it is not efficient. You should rely thread local for hot paths.

use std::time::Duration;

use futures::channel::oneshot;

#[monoio::main]
async fn main() {
    let (tx, rx) = oneshot::channel::<u8>();
    let t = std::thread::spawn(move || {
        println!("remote thread created");
        let mut rt = monoio::RuntimeBuilder::<monoio::FusionDriver>::new()
            .build()
            .unwrap();
        rt.block_on(async move {
            let n = rx.await;
            println!("await result: {n:?}");
        });
        println!("remote thread exit");
    });

    std::thread::sleep(Duration::from_secs(1));
    println!("send local: {:?}", tx.send(1));
    println!("wait for remote thread");
    let _ = t.join();
}