Class in task compiler

Channel

class Channel<T>
Import
use task::{Channel};
Source: compiler/src/prelude/task.hy

A bounded queue for passing values between tasks.

send waits while the channel is full and recv while it is empty, so a channel also paces producers and consumers. Values are shared, not copied (tasks share the heap). For OS threads, use thread::channel.

Methods#

NameDescription
sendAdds value to the channel, waiting while it is full (a suspension point).
try_sendAdds value if there is room, without waiting.
recvTakes the oldest value, waiting while the channel is empty (a suspension point).
try_recvTakes the oldest value if there is one, without waiting.
closeCloses the channel: no more values can be sent.
lenReturns the number of values waiting to be received.
capacityReturns the most values the channel can hold.
is_closedReturns true once close has been called.

Examples#

use task::{scope, channel, Scope, Channel};
use tests::assert;
let sum = scope(fn (Scope s) {
    let ch: Channel<int> = channel(2);
    let producer = s.spawn(fn () use (ch) {
        let i = 1;
        while i <= 5 {
            ch.send(i) ?? ();
            i = i + 1;
        }
        ch.close();
    });
    let total = 0;
    while let Result::Ok(v) = ch.recv() {
        total = total + v;
    }
    producer.join() ?? ();
    return total;
});
assert!((sum ?? 0) == 15);

See also#

↑ ↓ navigate ↵ open