GithubHelp home page GithubHelp logo

Comments (1)

BurntSushi avatar BurntSushi commented on July 1, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.