extern crate multiqueue;
extern crate crossbeam;
use self::multiqueue::{broadcast_queue};
use self::crossbeam::scope;
fn spsc_example() {
let (send, recv) = broadcast_queue(4);
scope(|scope| {
scope.spawn(move || for val in recv {
println!("Got {}", val);
});
for i in 0..10 {
loop {
if send.try_send(i).is_ok() {
break;
}
}
}
drop(send);
});
}
fn spsc_bcast_example() {
let (send, recv) = broadcast_queue(4);
scope(|scope| {
for i in 0..2 {
let cur_recv = recv.add_stream();
for j in 0..2 {
let stream_consumer = cur_recv.clone();
scope.spawn(move || for val in stream_consumer {
println!("Stream {} consumer {} got {}", i, j, val);
});
}
}
recv.unsubscribe();
for i in 0..10 {
loop {
if send.try_send(i).is_ok() {
break;
}
}
}
drop(send);
});
}
fn spmc_bcast_example() {
let (send, recv) = broadcast_queue(4);
scope(|scope| {
for i in 0..2 {
let cur_recv = recv.add_stream();
for j in 0..2 {
let stream_consumer = cur_recv.clone();
scope.spawn(move || for val in stream_consumer {
println!("Stream {} consumer {} got {}", i, j, val);
});
}
}
recv.unsubscribe();
for i in 0..10 {
loop {
if send.try_send(i).is_ok() {
break;
}
}
}
drop(send);
});
}
fn wacky_example() {
let (send, recv) = broadcast_queue(4);
scope(|scope| {
for i in 0..2 {
let cur_recv = recv.add_stream();
for j in 0..2 {
let stream_consumer = cur_recv.clone();
scope.spawn(move || for val in stream_consumer {
println!("Stream {} consumer {} got {}", i, j, val);
});
}
}
let single_recv = recv.add_stream().into_single().unwrap();
scope.spawn(move || for val in single_recv.iter_with(|item_ref| 10 * *item_ref) {
println!("{}", val);
});
let single_recv_2 = recv.add_stream().into_single().unwrap();
scope.spawn(move || for val in
single_recv_2.try_iter_with(|item_ref| 10 * *item_ref) {
println!("{}", val);
});
recv.unsubscribe();
for _ in 0..3 {
for i in 0..10 {
loop {
if send.try_send(i).is_ok() {
break;
}
}
}
}
drop(send);
});
}
fn main() {
println!("SPSC example");
spsc_example();
println!("\n\nSPSC Broadcast example");
spsc_bcast_example();
println!("\n\nSPMC Broadcast example");
spmc_bcast_example();
println!("\n\nWacky example");
wacky_example();
}