英文:
Rename 'main' thread in Rust
问题
I am working on an application that executes many processes, which at the same time use many threads. When something fails and the thread panics, rust outputs: thread 'main' panicked at 'explicit panic'
. I would like to use this messages for debugging purposes, being able to identify the panicking process by its name, but in this case all processes have the 'main' thread name.
How can I change the main thread name?
I know I can change the name of a child thread using the std::thread::Builder
, but this does not work for the current thread.
I have also tried using pthread_setname_np
call (I am using a Linux system):
use libc::{pthread_self, pthread_setname_np};
use std::{ffi::CString, thread};
fn main() {
// Set the name of the main thread
let name = CString::new("new_name").unwrap();
unsafe {
pthread_setname_np(pthread_self(), name.as_ptr());
}
panic!();
}
Executing this with cargo run
still panics with the main
name.
英文:
I am working on an application that executes many processes, which at the same time use many threads. When something fails and the thread panics, rust outputs: thread 'main' panicked at 'explicit panic'
. I would like to use this messages for debugging purposes, being able to identify the panicking process by its name, but in this case all processes have the 'main' thread name.
How can I change the main thread name?
I know I can change the name of a child thread using the std::thread::Builder
, but this does not work for the current thread.
I have also tried using pthread_setname_np
call (I am using a Linux system):
use libc::{pthread_self, pthread_setname_np};
use std::{ffi::CString, thread};
fn main() {
// Set the name of the main thread
let name = CString::new("new_name").unwrap();
unsafe {
pthread_setname_np(pthread_self(), name.as_ptr());
}
panic!();
}
Executing this with cargo run
still panics with the main
name.
答案1
得分: 3
I'm afraid this is impossible: the thread name is managed independently from the OS, and it is set only in two places in the standard library: when setting up main()
, and when spawning a thread. There is no public API that changes it for the current thread (you can ask for one if you believe this is important).
You can spawn a new thread and immediately join()
it.
英文:
I'm afraid this is impossible: the thread name is managed independently from the OS, and it is set only in two places in the standard library: when setting up main()
, and when spawning a thread. There is no public API that changes it for the current thread (you can ask for one, if you believe this is important).
You can spawn a new thread and immediately join()
it.
答案2
得分: 2
以下是您要翻译的内容:
一种在确定哪个进程崩溃的方法是设置自定义的 panic 处理程序。这在 Deno 运行时中有一个很好的示例:
https://github.com/denoland/deno/pull/13145/files
以下是如何执行的简短示例:
fn main() {
set_panic_hook(format!("程序 {}", 1));
panic!("发生了一些糟糕的事情");
}
fn set_panic_hook(process_name: String) {
let orig_hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |panic_info| {
eprintln!("\nPanic in process: {}", process_name);
orig_hook(panic_info);
std::process::exit(1);
}));
}
显示以下输出:
Panic in process: 程序 1
thread 'main' panicked at '发生了一些糟糕的事情', src/main.rs:3:5
英文:
One way to put detail on which process crashed is to set up a custom panic handler. A good example of this is in the Deno runtime:
https://github.com/denoland/deno/pull/13145/files
Here's a short sample of how to do it:
fn main() {
set_panic_hook(format!("Program {}", 1));
panic!("something bad happened");
}
fn set_panic_hook(process_name: String) {
let orig_hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |panic_info| {
eprintln!("\nPanic in process: {process_name}");
orig_hook(panic_info);
std::process::exit(1);
}));
}
Displays this output:
Panic in process: Program 1
thread 'main' panicked at 'something bad happened', src/main.rs:3:5
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论