Fan-in and channel shutdown
This example forwards values from two input channels into one bounded output. Each worker uses recv() so Some(0) is data and None means the source is closed and drained. One coordinator waits for both workers before closing the output. The consumer drains until None and then waits for the coordinator.
Save the following as fan-in.vx:
fn forward(source: Channel<i32>, output: Channel<i32>, finished: Channel<i32>) {
loop {
match source.recv() {
Some(value) => { if !output.send(value) { break; } },
None => { break; },
}
}
finished.send(1);
}
fn fanIn(left: Channel<i32>, right: Channel<i32>): (Channel<i32>, Channel<i32>) {
let output = Channel.new<i32>(1);
let workersDone = Channel.new<i32>(2);
let finished = Channel.new<i32>(1);
go { forward(left, output, workersDone); };
go { forward(right, output, workersDone); };
go {
let _ = workersDone.recv();
let _ = workersDone.recv();
output.close();
finished.send(1);
};
return (output, finished);
}
fn main(): i32 {
let left = Channel.new<i32>(1);
let right = Channel.new<i32>(1);
left.send(0); left.close();
let (output, finished) = fanIn(left, right.clone());
right.send(7); right.close();
let! count = 0;
let! sum = 0;
let! zero = 0;
let! seven = 0;
loop {
match output.recv() {
Some(value) => { count += 1; sum += value; if value == 0 { zero += 1; } if value == 7 { seven += 1; } },
None => { break; },
}
}
let _ = finished.recv();
$println(count); $println(sum); $println(zero); $println(seven);
if count != 2 || sum != 7 || zero != 1 || seven != 1 { return 1; }
return 0;
}Run vex run fan-in.vx. The result is independent of which worker sends first:
2
7
1
1The lines are total messages, their sum, number of zero values, and number of seven values. The left input closes before the right input sends; the right remains usable and both are drained before the output closes. The checks in main return a nonzero status if those counts differ.
Cancellation and bounded queues
The output buffer has capacity one, so a forwarding worker can wait when the consumer stops reading. In a design where the caller owns cancellation, close the output to wake or reject blocked sends, and coordinate the input owners so they stop producing and close the inputs to wake blocked receives. A producer whose send returns false must stop; closing an input while another owner still intends to send changes that producer's outcome. Wait for finished before treating the forwarding tasks as finished. Cancellation can discard values the consumer chose not to drain.
Cancellation needs its own notification or context-aware protocol. Decide whether outstanding messages are drained or discarded, notify every blocked participant, and wait for its completion acknowledgement. Arrival order can differ between producers; attach a source identifier or sequence number when the consumer needs to reconstruct that order.
select receives that return only T do not by themselves distinguish a closed channel from a valid zero value. Do not repeatedly select such a closed input as if every zero were a message. See Channels for receive and close semantics.
Related workflow
For a fixed number of jobs whose replies may succeed or fail, see Collecting Fallible Task Results.