Function in thread compiler

try_recv

fn try_recv<T>(Receiver rx) -> Result<T, ThreadError>
Import
use thread::{try_recv};
Source: compiler/src/builtin_docs/thread.hy

Takes the next value if there is one, without waiting.

Returns Err(ThreadError::WouldBlock) when the channel is empty but open, and Err(ThreadError::Disconnected) when it is empty and closed.

Parameters#

NameType
rxReceiver

Returns#

Result<T, ThreadError>

Examples#

use thread::{channel, send, try_recv, ThreadError};
use tests::assert;
let (tx, rx) = match channel() {
    Result::Ok(pair) => pair,
    Result::Err(_) => panic "channel failed",
};
let empty = match try_recv(rx) {
    Result::Err(ThreadError::WouldBlock) => true,
    default => false,
};
assert!(empty);
let _ = send(tx, 3);
assert!(match try_recv(rx) { Result::Ok(v) => v == 3, Result::Err(_) => false });

See also#

  • spawn: Runs f(arg) on another OS thread and returns a handle to it.
  • join: Waits for the thread to finish and returns the value its function returned.
  • detach: Lets the thread run to completion without being joined.
  • channel: Creates an unbounded channel and returns its (Sender, Receiver) ends.
  • send: Sends a copy of value on the channel.
  • recv: Takes the next value from the channel, waiting until one arrives.
  • try_send: Sends value without waiting.
  • close: Closes the channel: no more sends.
↑ ↓ navigate ↵ open