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. [Dynamic Language] pyspark Python3.7环境设置 及py4j.protocol.Py4JJavaError: An error occurred while calling z:org.apache.spark.api.python.PythonRDD.collectAndServe解决!

    pyspark Python3.7环境设置 及py4j.protocol.Py4JJavaError: An error occurred while calling z:org.apache.spa ...

  2. 关于ClickOnce的一些技术文章

    程序自动升级是我们经常遇到的需求,对于.Net程序来说,一个简单易用的方案是它内置的ClickOnce技术.ClickOnce出现的比较早,网上相应的教程还是比较丰富的,我这里就简单的整理一下相关的文 ...

  3. Syncovery : Google Docs protocol completely replaced with Google Drive

    Google Docs protocol completely replaced with Google Drive In May 2015, the older Google Docs API wa ...

  4. Reverse Engineering the NC ECU (revisited) -- SH7508

    http://forum.miata.net/vb/showthread.php?t=536601 Hey all! About 5 years ago, there was a great thre ...

  5. jsondataobjects

    jsondataobjects GITHUB: https://github.com/ahausladen/jsondataobjects.git 跨平台JSON库 Json Data Objects ...

  6. arcgis runtime 100 Create geometries

    1 /* Copyright 2016 EsriEsri 2 * 3 * Licensed under the Apache License, Version 2.0 (the "Licen ...

  7. .NET:CLR via C#:CLR Hosting And AppDomains

    AppDomain Unloading To unload an AppDomain, you call AppDomain’s Unload static method.This call caus ...

  8. pytest文档23-使用多个fixture和fixture直接互相调用

    使用多个fixture 如果用例需要用到多个fixture的返回数据,fixture也可以return一个元组.list或字典,然后从里面取出对应数据. # test_fixture4.py impo ...

  9. Eclipse——浏览功能

    一,打开变量声明 或选择opendeclaration就能够查看变量的定义 二.打开类型层次结构(open type hierarchy) 或者点击F4 watermark/2/text/aHR0cD ...

  10. Java遍历Map键、值。获取Map大小的方法

    Map读取键值对,Java遍历Map的两种实现方法 第一种方法是根据map的keyset()方法来获取key的set集合,然后遍历map取得value的值 import java.util.HashM ...