Skip to content

Example of blocking usage pattern with new API  #69

Description

@rnestler

I previously (0.4.1) used the sync API, where I could just do a let msg = rx.try_recv().unwrap(); and the unwrap would only fail if the other end disconnected.

My use case is that I have a producer thread and several consumer threads that are just waiting for messages.

How is the new API intended to be used? Currently I do something like the following:

loop {
            let msg = match self.rx.try_recv() {
                Ok(msg) => msg,
                Err(channel::TryRecvError::Empty) => continue,
                Err(channel::TryRecvError::Disconnected) => panic!("Sender disconnected"),
            };
}

On the receiver which seems to work, but uses full CPU, so probably I should add a sleep in the Empty branch:

loop {
            let msg = match self.rx.try_recv() {
                Ok(msg) => msg,
                Err(channel::TryRecvError::Empty) => {
                    sleep(Duration::from_millis(1));
                    continue;
                },
                Err(channel::TryRecvError::Disconnected) => panic!("Sender disconnected"),
            };
}

Is this how the API should be used? Or should I use a different crate for my use case?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions