Comments (1)
My primary problem with this issue is that you've built a contrived example. Because of that, it's super hard to give you a good answer. It would be much better if you could focus on the specific problem you want to solve. I'll try, though...
In this case, you really need to build termination into the logic of your program. Since the value received by each channel is an option, you can quit your loop once both channels have been closed.
This is what I came up with:
#[macro_use]
extern crate chan;
use std::thread;
fn main() {
let (s1, r1) = chan::async();
let (s2, r2) = chan::async();
thread::spawn(move || run(5, s1));
thread::spawn(move || run(10, s2));
let mut found1 = 0;
let mut found2 = 0;
let (mut r1closed, mut r2closed) = (false, false);
while !r1closed || !r2closed {
chan_select! {
r1.recv() -> n => {
found1 += 1;
println!("Got r1: {:?} ({})", n, found1);
if n.is_none() {
r1closed = true;
}
},
r2.recv() -> n => {
found2 += 1;
println!("Got r2: {:?} ({})", n, found2);
if n.is_none() {
r2closed = true;
}
},
}
}
// We'll obviously never get here
println!("R1s: {} / R2s: {}", found1, found2);
}
fn run(n: u8, s: chan::Sender<u8>) {
for i in 0..n {
s.send(i * n);
}
}
I don't quit grok your concerns about CPU usage. Certainly, in the specific code you've given me, it's not a problem.
from chan.
Related Issues (19)
- remove hyper dev dependency HOT 4
- Possible memory leak in `tick` HOT 2
- [feature request] re-expose try_recv() HOT 8
- Using chan_select! requires values to be 'static HOT 3
- Add timeout-able methods HOT 1
- doesn't seem to work with `fork()` HOT 2
- Select integration with std lib mpsc api HOT 4
- Allow chan_select to generate no-op None variants HOT 2
- Sender::send(t) -> Result<(), SendError> HOT 16
- Deadlock with select-send and select-recv HOT 2
- crossbeam-channel – EOL chan HOT 6
- Broadcast to all consumers HOT 4
- Return value from `chan_select!` expression HOT 2
- chan::async() conflicts with Rust 2018 Edition HOT 6
- I think it's butter to return a Result for invalid add HOT 5
- Relicense under dual MIT/Apache-2.0 HOT 8
- Async channel is blocking program HOT 9
- All Notifier subscriptions made in select.rs appear to be leaked HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chan.