Add watch channel with latest-value semantics#1241
Open
veeceey wants to merge 2 commits into
Open
Conversation
Implements a watch channel where send never blocks (overwrites the latest value) and recv blocks until a new value arrives. Uses Mutex<Option<T>> + Condvar internally. Supports multiple senders and receivers (cloneable), with proper disconnect detection. Closes crossbeam-rs#942
Author
|
Test results: Full existing test suite also passes (74 doc+unit tests). |
Author
|
checking in on this - the watch channel is a pretty commonly requested primitive. open to design feedback |
Author
|
friendly bump on this - happy to make any design changes if needed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a
watchchannel type that stores only the latest value. Sending never blocks (overwrites the previous value), and receiving blocks until a new value is available.This is useful for scenarios like refresh signals where you only care about the most recent value - if multiple sends happen while the receiver is busy, the receiver processes just one iteration instead of N.
The implementation uses
Mutex<Option<T>> + Condvaras suggested in #942. Key points:WatchSenderandWatchReceiverare bothCloneRecvErrorwhen all senders are dropped, send returnsSendErrorwhen all receivers are dropped)try_recvis available for non-blocking checksCloses #942