为什么在linux信号事件中未使用静态成员

Viv*_*oel 1 c++ linux static

我的班级如下:

AppStart{
static void SignalHandler(int signal){
    std::cout<< log_file_;
}
static void Start(){
//Set log_file_ path 
//Register signal handler 
}
static string log_file_;
}
Run Code Online (Sandbox Code Playgroud)

当第一次通过使用kill命令发送信号来调用SignalHandler时,它正在打印正确的值.但是当它被多次调用时,它会给出未定义的行为.为什么静态变量在完成程序执行之前就被破坏了?

Jas*_*son 5

调用operator<<()std::cout不是台异步信号安全功能.所以,你不应该使用它的信号处理程序内部.任何使用都将调用未定义或最好的不确定行为.您可以write()在我提供的链接中看到可能的输出候选候选列表,例如低级别等.