Advertisement

Python notes: 正确响应Ctrl-C信号以实现优雅退出多线程进程

阅读量:

熟悉C/C++语言开发服务端程序的工程师,对于通过捕捉Ctrl-C信号(即SIG_TERM信号量)来实现多线程C程序的优雅终止机制应当较为了解,其常规实现方式的示例代码大致如下:

复制代码
    #include <signal.h>
    
    int main(int argc, char * argv[]) 
    {
    // 1. do some init work 
    ... init() ...
    
    // 2. install signal handler, take SIGINT as  example install:
    struct sigaction action;
    memset(&action, 0, sizeof(struct sigaction));
    action.sa_handler = term;
    sigaction(SIGTERM, &action, NULL);
    
    // 3. create thread(s)
    pthread_create(xxx);
    
    // 4. wait thread(s) to quit
    pthread_join(thrd_id);
    
    return 0;
    }
    
      

全部评论 (0)

还没有任何评论哟~