-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_thread_names.rs
More file actions
51 lines (42 loc) · 1.57 KB
/
test_thread_names.rs
File metadata and controls
51 lines (42 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::sync::atomic::{AtomicUsize, Ordering};
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Testing thread naming...");
// Test Rayon global pool
rayon::ThreadPoolBuilder::new()
.thread_name(|i| format!("rayon-global-{}", i))
.num_threads(4)
.build_global()?;
// Test Tokio runtime
let tokio_counter = AtomicUsize::new(0);
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.thread_name_fn(move || {
let counter = tokio_counter.fetch_add(1, Ordering::SeqCst);
format!("tokio-test-{}", counter)
})
.worker_threads(4)
.build()?;
println!("Thread pools created!");
println!("Process PID: {}", std::process::id());
println!("\nCheck thread names with:");
println!(" ps -T -p {} -o pid,tid,comm", std::process::id());
runtime.block_on(async {
// Spawn some work to ensure threads are created
let handles: Vec<_> = (0..4).map(|i| {
tokio::spawn(async move {
tokio::time::sleep(tokio::time::Duration::from_secs(2)).await;
println!("Tokio task {} done", i);
})
}).collect();
for h in handles {
h.await.ok();
}
// Spawn rayon work
rayon::spawn(|| {
std::thread::sleep(std::time::Duration::from_secs(1));
println!("Rayon task done");
});
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
});
Ok(())
}