use std::collections::HashMap;
use std::hint::black_box;
use std::thread;
use alloc_tracker::{Allocator, Report, Session};
#[global_allocator]
static ALLOCATOR: Allocator<std::alloc::System> = Allocator::system();
fn main() {
println!("=== Threaded Allocation Tracking Example ===");
println!();
let handle1 = thread::spawn(|| worker_thread("Thread-1"));
let handle2 = thread::spawn(|| worker_thread("Thread-2"));
let report1 = handle1
.join()
.expect("Thread-1 should complete successfully");
let report2 = handle2
.join()
.expect("Thread-2 should complete successfully");
println!("Report from Thread-1:");
report1.print_to_stdout();
println!();
println!("Report from Thread-2:");
report2.print_to_stdout();
println!();
let merged_report = Report::merge(&report1, &report2);
println!("=== Merged Report ===");
println!("This shows combined statistics from both threads:");
println!("- 'common_work' operations are merged (both threads recorded this)");
println!("- 'unique_work' operations appear separately (each thread recorded different ones)");
println!();
merged_report.print_to_stdout();
println!();
println!("✓ Successfully demonstrated thread-safe report merging");
println!("✓ Reports contain both same-operation merging and different-operation data");
}
fn worker_thread(thread_name: &str) -> Report {
println!("🧵 {thread_name} starting work...");
let session = Session::new();
{
let common_op = session.operation("common_work");
let _span = common_op.measure_thread().iterations(3);
for i in 0..3 {
let data = format!("{thread_name} iteration {i}: ");
let mut strings = Vec::new();
for j in 0..50 {
strings.push(format!("{data} string {j}"));
}
black_box(strings);
}
}
let unique_work_name = format!("unique_work_{thread_name}");
{
let unique_op = session.operation(&unique_work_name);
let _span = unique_op.measure_thread().iterations(2);
for i in 0_u32..2 {
if thread_name == "Thread-1" {
let mut vecs = Vec::new();
for j in 0_u32..20 {
vecs.push(vec![
i,
j,
i.checked_add(j)
.expect("safe arithmetic: small loop indices cannot overflow"),
]);
}
black_box(vecs);
} else {
let mut map = HashMap::new();
for j in 0_u32..30 {
map.insert(
format!("{thread_name}-{i}-{j}"),
i.checked_add(j)
.expect("safe arithmetic: small loop indices cannot overflow"),
);
}
black_box(map);
}
}
}
println!("🧵 {thread_name} completed work");
session.to_report()
}