-
Notifications
You must be signed in to change notification settings - Fork 341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Zero-capacity channels #436
Comments
@stjepang you obviously know better, but I find it quite amusing that there has to be a difference between threads and tasks here. Like, the threads don't magically rendezvous at a single moment in time (you might have only a single CPU). Could you point out exactly why can't we deem the operation completed only after both sides made all the required moves? EDIT: go read the sources is also a great answer here, if that's the easiest way to explain :) |
It's the same reason why scoped threads are easy to design, but scoped tasks (impossibly?) difficult. The crucial difference is that we can force threads to complete operations, whereas we can't force futures:
The guarantees I want to uphold are these:
That's not possible with zero-capacity channels, as far as I can tell. What I'm proposing in this issue is that we drop the first guarantee and just live with that decision. The problem is that as both sides are making moves, we'll always reach a point where one side says "ok I've polled the future and it has returned |
zero capacity channels are possible with futures. You just need to rendezvous on the Edit: I agree that it doesn't seem possible using the current definition of the |
How about faithfully simplify the mpmc channel for the capacity = 1 case, then just call it one-capacity channel? Then |
While we can't implement zero-capacity channels based on futures as faithfully as they are in
crossbeam-channel
and Go, we can do something very similar. Here's a proposal.Our zero-capacity channel could be implemented as a channel that has the capacity of 1, except that every send operation does not complete until its message is received.
The problem here is that send operations are not cleanly cancelable. Once a send operation has put its message into the channel, a receive operation can take it. If the
Send
future is then dropped, the send operation is not really canceled because someone has already picked up the message.This issue will come up if we're selecting over send operations:
Here it's possible for both send operations to be executed simultaneously. But maybe that's fine and not a big issue since selection over send operation is rare.
Fortunately, selection over receive operation is still cleanly cancellable:
Exactly one receive operation will complete here, which is what the user would probably expect. We can't make the same guarantee about send operations on zero-capacity channels, though.
Is this an acceptable compromise? What does everyone think?
cc @matklad
The text was updated successfully, but these errors were encountered: