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. faceNet编译问题

    1.执行align_dataset_mtcnn.py出现无法导入检测模型的问题 a.现象如下 Creating networks and loading parameters Traceback (m ...

  2. 手把手教你搭建nuget服务器

    新建web项目 工具:VS2013 版本:.Net Framework 4.6,低版本也行,不过要找到对应版本的Nuget.Server 装了NuGet客户端(百度如何安装) WebForm或MVC都 ...

  3. PHP常量定义之define与const对比

    简要归纳PHP中两个常量定义关键字的区别: 1.define是函数,const是语言结构,PHP编译时const速度快.2.define只能用在类外,const类内类外皆可.3.define定义的常量 ...

  4. MySql篇

    CentOS6下通过yum安装的MySQL是5.1版的,比较老,所以就想通过源代码安装高版本的5.6.26. 一:卸载旧版本 使用下面的命令检查是否安装有MySQL Server rpm -qa | ...

  5. acm省赛选拔组队赛经验谈

    省赛组队赛已经进行5场了,过半了. 从曾经的不会组队到如今逐渐磨合,尽管每次都有遗憾,可是我认为我们一直在进步.有些失误是要记录下来下次不能再犯的! 经验: 1:上场開始一定要有人(英语能力和算法综合 ...

  6. easyui中combobox 验证输入的值必须为选项框中的数据

    当作为提示框的方式时,combobox必须设置为允许用户输入的模式,但是当用户输入后未选择正确的数据就直接按tab或点击鼠标离开控件会导致用户输入无效的值并且通过验证,为了避免这种情况的发生我们需要对 ...

  7. 在gdb将所有线程的堆栈输出到文件中去

    http://m.blog.csdn.net/blog/lantianjialiang/40111253

  8. AMD64 Instruction-Level Debugging With dbx

    http://www.oracle.com/technetwork/server-storage/solarisstudio/documentation/amd64-dbx-364568.html A ...

  9. error: conflicting type qualifiers for &#39;xxxxx&#39;

    网上的非常多解释--非常难理解-- 情景描写叙述: 在代码中,写了A\B两个文件,A:是.c文件,B是.h文件和.c文件. 在A中包括了B.h文件. 在B.h文件里声明了A中定义的变量. 这样编译的情 ...

  10. 用Python语言写Hadoop MapReduce程序Writing an Hadoop MapReduce Program in Python

    In this tutorial I will describe how to write a simple MapReduce program for Hadoop in the Python pr ...