channels/channels.rs
1//! Example of having a cleanup thread.
2//!
3//! In some instances objects may be expensive to delete. Sending objects to a dedicated cleanup
4//! thread can alleviate the load on the workers.
5//!
6//! The sender thread writes key/value pairs from 0 through 19 with a max cachemap size of 10. This
7//! will result in elements 0 through 9 being pushed out and sent on the channel where they are
8//! written to stdout.
9
10use evicting_cache_map::EvictingCacheMap;
11use std::sync::mpsc;
12use std::thread;
13
14fn main() {
15 let (tx, rx) = mpsc::channel();
16 let mut cachemap: EvictingCacheMap<String, u8, 10, _> =
17 EvictingCacheMap::with_prune_hook(move |k, v| tx.send((k, v)).unwrap());
18 let send = thread::spawn(move || {
19 for x in 0..20 {
20 cachemap.insert(x.to_string(), x)
21 }
22 });
23
24 let recv = thread::spawn(move || {
25 while let Ok((k, v)) = rx.recv() {
26 println!("{k}:{v}")
27 }
28 });
29
30 let _ = send.join();
31 let _ = recv.join();
32}