1

In rust, is there any guarantee that, with a channel created via

let (tx_net, rx_net) = mpsc::sync_channel::<Something>(1024);

that a message sent on the otherwise-empty channel shall be immediately available to [at least one] recv()?

The scenario in question is a test where a [single-threaded] routine calls send() (in the context of some crate) and then [nearly immediately] calls try_recv() where an empty return would indicate a failure. The question came up as to whether this represented a race condition. In looking at the current channel implementation, the answer is "no". But I couldn't find anything in the crate documentation that makes this promise.

N.B.: non-zero capacity channel with single-consumer

4
  • It does sound racy. If you're worried about that, why not just use recv, which blocks until data is available or returns Err if there will never be more data (all senders disconnected and channel is empty). Commented Sep 2 at 16:15
  • That doesn't sound risky to me, but you can request T-libs-api to guarantee it. Commented Sep 2 at 16:25
  • @BallpointBen in this case, the sender is still connected (by virtue of being in a crate that is a "black box"). Commented Sep 2 at 18:36
  • @ChayimFriedman -- thanks for the tip: github.com/rust-lang/rust/issues/146135 Commented Sep 2 at 20:57

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.