I faced some interesting behavior using tokio's channel - tokio::sync::mpsc
.
Long story short: in some cases, receiver
doesn't get the value, sent by the sender
if there's something like loop (or any other heavy computations, i assume?) on the current tokio-thread.
Channel from std::thread::sync
works perfectly though. I assume it has something to do with either tokio scheduler or cpu context switch ?
Even in the example below it works correctly from time to time. But sometimes it prints "second thread" only when the first thread is dead due to integer overflow.
How can i overcome this problem ? For now i use default channel()
from std::thread::sync
. But it seems like a crutch to me.
Example:
use tokio::sync::mpsc;
async fn thread1(mut sender: mpsc::Sender<bool>) {
let mut i = 0;
loop {
i += 1;
if i == 100 {
sender.send(true).await.unwrap();
}
}
}
async fn thread2(mut receiver: mpsc::Receiver<bool>) {
receiver.recv().await.unwrap();
println!("second thread");
}
#[tokio::main]
async fn main() {
let (sender, receiver) = mpsc::channel(1);
let p1 = tokio::spawn(async move {
thread1(sender).await;
});
let p2 = tokio::spawn(async move {
thread2(receiver).await;
});
tokio::join!(p1, p2);
}
Thanks in advance!