thread_safety/
thread_safety.rs

1use hackerlog::*;
2use std::thread;
3use std::time::Duration;
4
5fn main() {
6    // Enable verbose mode to see thread information
7    logger().verbose(true);
8
9    // Spawn multiple threads that log simultaneously
10    let mut handles = vec![];
11
12    for i in 0..3 {
13        let handle = thread::spawn(move || {
14            let _ctx = logger().add_context("thread_id", i.to_string());
15
16            for j in 0..3 {
17                info!("Message {} from thread {}", j, i);
18                thread::sleep(Duration::from_millis(100));
19            }
20        });
21
22        handles.push(handle);
23    }
24
25    // Wait for all threads to finish
26    for handle in handles {
27        handle.join().unwrap();
28    }
29}