I want to simplify my code by accessing my shared memory shared
synchronously.
The elsewhere::run function waits for any request from the function sender running in another thread.
The requests come as structs of different types (Foo
& Bar
) send via async_channel
.
Each request has a different behaviour on the shared memory.
I think it is possible to bind different call to async_channel::Receiver::recv()
with the use of futures::future::select_all
but i am failing to figure it out.
Binding both receiver allows to wait on both at the same time and resume the task on the first received msg while being synchronous.
How can i manage to receive from different async_channel of different type at the same type? (below a minimum reproducible example)
use async_channel::Sender;
use std::{thread, time};
use futures::executor::block_on;
pub struct Foo {}
pub struct Bar {}
pub mod elsewhere {
use futures::future::select_all;
use crate::{Foo, Bar};
use async_channel::Receiver;
enum FooBar {
Foo(Foo),
Bar(Bar)
}
pub async fn run(rx_foo: Receiver<Foo>, rx_bar: Receiver<Bar>) {
println!("run is listening");
let mut shared: Vec<i32> = vec![0]; // Try to avoid having a mutex on shared memory by accessing synchronously
loop {
let _recv_foo = rx_foo.recv();
let _recv_bar = rx_bar.recv();
/**
* [...]
* Cast Foo and Bar into a FooBar and use something like "select_all" to wait for
* the first received element of the two channels rx_foo & rx_bar
*/
let firstReceived = FooBar::Foo(Foo{}); // Assign a dummy value instead of the channels msg for compiling the example
match firstReceived {
Foo => { shared.push(0);}
Bar => { shared.clear();}
}
}
}
}
async fn sender(tx_foo: Sender<Foo>, tx_bar: Sender<Bar>) {
println!("Send to run");
let _ = tx_foo.send(Foo{}).await;
let _ = tx_bar.send(Bar{}).await;
}
fn main() {
// I want the main module to be agnostic of elsewhere::FooBar
let (tx_foo, rx_foo) = async_channel::unbounded::<Foo>();
let (tx_bar, rx_bar) = async_channel::unbounded::<Bar>();
thread::spawn(move || block_on(elsewhere::run(rx_foo, rx_bar)));
thread::spawn(move || block_on(sender(tx_foo, tx_bar)));
thread::sleep(time::Duration::from_secs(10));
}