Crates.io | thread-broadcaster |
lib.rs | thread-broadcaster |
version | 0.1.0 |
source | src |
created_at | 2023-06-12 20:59:19.913404 |
updated_at | 2023-06-12 20:59:19.913404 |
description | A SPMC (Single producer multi consumer) broadcasting channel to broadcast notifications between threads |
homepage | https://github.com/DarthBenro008/thread-broadcaster |
repository | https://github.com/DarthBenro008/thread-broadcaster |
max_upload_size | |
id | 888477 |
size | 14,549 |
Thread Broadcaster is a Single Channel Multi-Producer (SPMC) library that enables the sending of notifications between threads. Unlike most Multi-Producer Multi-Consumer (MPMC) implementations, Thread Broadcaster ensures that all listeners receive the data, rather than just the first one.
tracing
for debugsuse core::time;
use std::thread;
use thread_broadcaster::{BroadcastListener, Broadcaster};
fn main() {
let (b, s) = Broadcaster::<String>::new();
let s2 = s.clone();
thread::spawn(move || {
let ls1 = BroadcastListener::register_broadcast_listener(s);
for msg in ls1.channel {
println!(
"got broadcast with data: {} on thread {:#?}",
msg,
thread::current().id()
);
}
});
thread::spawn(move || {
let ls2 = BroadcastListener::register_broadcast_listener(s2);
for msg in ls2.channel {
println!(
"got broadcast with data: {} on thread {:#?}",
msg,
thread::current().id()
);
}
});
thread::spawn(move || {
// we wait for registration
thread::sleep(time::Duration::from_secs(1));
b.broadcast("something to broadcast".to_string());
// we wait for listeners to pickup before being dropped
thread::sleep(time::Duration::from_secs(2));
})
.join()
.unwrap();
}
cargo add thread-broadcaster
Feel Free to Open a PR/Issue for any feature or bug(s).
Make sure you follow the community guidelines.
Feel free to open an issue to ask a question/discuss anything about melonpan.
Have a feature request? Open an Issue!
Copyright 2022 Hemanth Krishna
Licensed under MIT License : https://opensource.org/licenses/MIT
Made with ❤ and multiple cups of coffee