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 \n", signo);

    pthread_exit(NULL);

}

void* test(void *arg)

{

    signal(SIGQUIT,handle_quit );

    for(int i=0;i<100;i++)

    {

        printf("in pthread test \n");

        sleep(1);

    }

}

int main(void)

{

    printf("begin \n");

    pthread_create(&pid, NULL , test, NULL);

    sleep(3);

    if(pthread_kill(pid, 0)!= ESRCH)

    {

        printf("thread %d exists!\n", pid);

        pthread_kill(pid, SIGQUIT);

//        pthread_exit(NULL);//this won't work

        printf("after kill\n");

    }

    sleep(1);

    printf("exit in main\n");
}

如何不使用pthread_cancel而杀死线程的更多相关文章

  1. 线程相关函数(1)-pthread_create(), pthread_join(), pthread_exit(), pthread_cancel() 创建取消线程

    一. pthread_create() #include <pthread.h> int pthread_create(pthread_t *thread, const pthread_a ...

  2. linux下pthread_cancel无法取消线程的原因【转】

    转自:http://blog.csdn.net/huangshanchun/article/details/47420961 版权声明:欢迎转载,如有不足之处,恳请斧正. 一个线程可以调用pthrea ...

  3. linux下pthread_cancel无法取消线程的原因

    一个线程能够调用pthread_cancel终止同一进程中的还有一个线程,可是值得强调的是:同一进程的线程间,pthread_cancel向还有一线程发终止信号.系统并不会立即关闭被取消线程,仅仅有在 ...

  4. python中优雅的杀死线程

    上一篇博客中,杀死线程采用的方法是在线程中抛出异常   https://www.cnblogs.com/lucky-heng/p/11986091.html, 这种方法是强制杀死线程,但是如果线程中涉 ...

  5. python中杀死线程

    有时候有这样的需要,在某种情况下,需要在主线程中杀死之前创建的某个线程,可以使用下面的方法,通过调用python内置API,在线程中抛出异常,使线程退出. import threading impor ...

  6. python主动杀死线程

    简介 在一些项目中,为了防止影响主进程都会在执行一些耗时动作时采取多线程的方式,但是在开启线程后往往我们会需要快速的停止某个线程的动作,因此就需要进行强杀线程,下面将介绍两种杀死线程的方式. 直接强杀 ...

  7. mysql杀死线程

    查询 正在执行的事务:SELECT * FROM information_schema.INNODB_TRX 根据这个事务的线程ID(trx_mysql_thread_id): 可以使用mysql命令 ...

  8. mysql中show processlist过滤和杀死线程

    select * from information_schema.processlist where HOST LIKE '%192.168.1.8%'; kill ID列

  9. 线程取消 (pthread_cancel)

    线程取消(pthread_cancel) 基本概念pthread_cancel调用并不等待线程终止,它只提出请求.线程在取消请求(pthread_cancel)发出后会继续运行,直到到达某个取消点(C ...

随机推荐

  1. WIKIOI 1026 逃跑的拉尔夫 深度优先搜索

    /* 1026 逃跑的拉尔夫 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold       题目描述 Description 年轻的拉尔夫开玩笑地从一个小镇上偷走了一辆 ...

  2. Ubntu 14.04 下 开源骨架跟踪-skeltrack

    Skeltrack是个不错的开源骨架跟踪软件.跟踪起来还相对的稳定速度还不错.能满足基本的体感功能.下面来介绍下怎么安装.  1.运行环境配置 #need clutter 1.8 or greater ...

  3. git push时提示"fatal: The current branch master has no..."

    git push到远程仓库时提示:fatal: The current branch master2 has no upstream branch. To push the current branc ...

  4. 从零开始搭建linux下laravel 5.5所需环境(三)

    好的,我们已经安装好了nginx+mysql+php了,打开[ Laravel 5.5 文档 ] 快速入门 —— 安装配置篇 我们看到这里需要安装Composer,好的,我们现在就来安装Compose ...

  5. SmartProg2 Universal, ISP capable programmer

    http://www.elnec.com/products/universal-programmers/smartprog2/ 40 powerful TTL pindrivers provide H ...

  6. JTAG Pinouts

    http://www.jtagtest.com/pinouts/ Pinouts ARM-20 (used with almost all ARM-based microcontrollers) AR ...

  7. Iptables 指南 1.1.19

    Iptables 指南 1.1.19 Oskar Andreasson oan@frozentux.net Copyright © 2001-2003 by Oskar Andreasson 本文在符 ...

  8. Spring使用环境变量控制配置文件加载(转)

    项目中需要用到很多配置文件,不同环境的配置文件是不一样的,因此如果只用一个配置文件,势必会造成配置文件混乱,这里提供一种利用环境变量控制配置文件加载的方法,如下: 一.配置环境变量 如果是window ...

  9. DocumentManager在标签位置显示气泡框 z

    关于DevExpress DockManager下的DocumentManager头部标签如何显示气泡框,类似Visual studio那样显示文件的路径,如下图所示,------- 方式很简单,从工 ...

  10. leetCode解题报告5道题(九)

    题目一:Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ...