Skip to main content

bounded

Function bounded 

Source
pub fn bounded<T>(size: usize) -> (Sender<T>, Receiver<T>)
Expand description

Creates a new sync bounded channel with the requested buffer size, and returns Sender and Receiver of the channel for type T, you can get access to async API of AsyncSender and AsyncReceiver with to_sync, as_async or clone_sync based on your requirements, by calling them on sender or receiver.

ยงExamples

use std::thread::spawn;

let (s, r) = kanal_plus::bounded(0); // for channel with zero size queue, this channel always block until successful send/recv

// spawn 8 threads, that will send 100 numbers to channel reader
for i in 0..8{
    let s = s.clone();
    spawn(move || {
        for i in 1..100{
            s.send(i);
        }
    });
}
// drop local sender so the channel send side gets closed when all of the senders finished their jobs
drop(s);

let first = r.recv().unwrap(); // receive first msg
let total: u32 = first+r.sum::<u32>(); // the receiver implements iterator so you can call sum to receive sum of rest of messages
assert_eq!(total, 39600);