use pulse_map::ConcurrentPulseMap;
use std::sync::Arc;
use std::thread;
fn main() {
println!("═══════════════════════════════════════════");
println!(" ConcurrentPulseMap — Multi-threaded Example");
println!("═══════════════════════════════════════════\n");
let map = Arc::new(ConcurrentPulseMap::with_auto_resize(64));
let mut handles = vec![];
for thread_id in 0..4 {
let map = Arc::clone(&map);
handles.push(thread::spawn(move || {
for i in 0..250 {
let key = format!("t{}_key_{}", thread_id, i);
let val = format!("value_{}", i * thread_id);
map.insert(key, val);
}
println!(" Thread {} — wrote 250 entries", thread_id);
}));
}
for h in handles {
h.join().unwrap();
}
println!("\n Total entries: {}", map.len());
println!(" Capacity: {}", map.capacity());
println!(" Load factor: {:.1}%", map.load_factor() * 100.0);
println!(" Evictions: {}", map.eviction_count());
let mut readers = vec![];
for thread_id in 0..4 {
let map = Arc::clone(&map);
readers.push(thread::spawn(move || {
let mut found = 0;
for i in 0..250 {
let key = format!("t{}_key_{}", thread_id, i);
if map.get(&key).is_some() {
found += 1;
}
}
println!(" Reader {} — found {}/250", thread_id, found);
found
}));
}
let total_found: usize = readers.into_iter().map(|h| h.join().unwrap()).sum();
println!("\n Total reads: {}/1000", total_found);
println!("\n✅ Thread-safe operations complete!");
}