zoukankan      html  css  js  c++  java
  • 如何不使用pthread_cancel而杀死线程

    http://www.cnblogs.com/no7dw/archive/2012/09/27/2705847.html

    During the time I use standalone cross compliers to build my system, I find there is NO pthread_cancel in pthread.h (/home/dengwei/standalone-toolchain/sysroot/usr/include/pthread.h).

    Shocked by that, but here comes the solution, by using pthread_kill to send a signal , and adding a signal handler :

    #include <pthread.h>
    
    #include <stdio.h>
    
    #include <stdlib.h>
    
    #include <signal.h>
    
    #include <unistd.h>
    
    #include <sys/types.h>
    
    #include <errno.h>
    
    pthread_t pid;
    
    void handle_quit(int signo)
    
    {
    
        printf("in qq handle sig %d 
    ", signo);
    
        pthread_exit(NULL);
    
        
    
    }
    
    void* test(void *arg)
    
    {
    
        signal(SIGQUIT,handle_quit );
    
        for(int i=0;i<100;i++)
    
        {
    
            printf("in pthread test 
    ");
    
            sleep(1);
    
        }
    
    
    
    }
    
    int main(void)
    
    {
    
        printf("begin 
    ");
    
        pthread_create(&pid, NULL , test, NULL);
    
        sleep(3);
    
        if(pthread_kill(pid, 0)!= ESRCH)
    
        {
    
            printf("thread %d exists!
    ", pid);
    
            pthread_kill(pid, SIGQUIT);
    
    //        pthread_exit(NULL);//this won't work
    
            printf("after kill
    ");
    
        }
    
    
    
        sleep(1);
    
        printf("exit in main
    ");
    }
    
  • 相关阅读:
    iframe跨域
    changePage() 页面跳转
    APACHE启动失败是SYSTEM对apache目录没权限导致
    git使用中出现的错误
    python面试总结
    python面试30-40题
    python面试1-30题
    购物车的基本流程
    vue的基础知识
    三大框架的对比
  • 原文地址:https://www.cnblogs.com/jingzhishen/p/3468865.html
Copyright © 2011-2022 走看看