http://www.cnblogs.com/hnrainll/archive/2011/04/20/2022149.html

#include<stdlib.h>

#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
void clean_fun1(void * arg)
{
    printf("this is clean fun1\n");
}
void clean_fun2(void * arg)
{
    printf("this is clean fun2\n");
}
void * thread_fun(void * arg)
{
    pthread_cleanup_push(clean_fun1,NULL);
    pthread_cleanup_push(clean_fun2,NULL);
    sleep(100);
    //这里要注意,如果将sleep(100);换成while(1);的话,程序会一直暂停.push和pop要成对出现.
    //因为while(1);运行的太快,线程不接受cancel信号
    //while(1);
    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);
    return NULL;
}
int main()
{
    pthread_t tid1;
    int err;
    err=pthread_create(&tid1,NULL,thread_fun,NULL);
    if(err!=0)
    {
        perror("pthread_create");
        exit(0);
    }
    sleep(3);
    //printf("test\n");
    err=pthread_cancel(tid1);
    if(err!=0)
    {
        perror("cancel error:");
        exit(0);
    }
    err=pthread_join(tid1,NULL);
    if(err!=0)
    {
        perror("pthread_join  error:");
        exit(0);
    }
     
    return 0;
}

最新文章

  1. Amazon Dynamo论文学习
  2. sql行列转换
  3. openstack(liberty):部署实验平台(三,简单版本软件安装 之cinder,swift)
  4. 【JavaScript】强制缓存刷新
  5. vue2+webpack使用1--初识默认展示页面
  6. 路由知识 静态路由 rip eigrp ospf
  7. C#图解教程 第二十二章 异常
  8. 【Python全栈-HTML】HTML引入文件的绝对路径、相对路径、根目录
  9. 2个监听器+ dialog + replysubject + extends
  10. 2016/12/20 dplの课练
  11. 并发编程—— LinkedTransferQueue
  12. 统计Mongo数组中相同对象的属性之和
  13. 解决javamail ssl 测试unable to find valid certification path to requested target
  14. nodejs(一)process模块
  15. 2018.08.04 spoj TTM to the moon(主席树)
  16. 解决js跨域
  17. kvm虚拟机添加网卡
  18. BZOJ 3676: [Apio2014]回文串 后缀自动机 Manacher 倍增
  19. DICOMDIR结构
  20. vs 代码自动对其(注释,等号...)

热门文章

  1. PIE SDK图层属性
  2. vue父子组件生命周期函数执行顺序
  3. (转)创建DB2实例时出错,请大家帮忙解决
  4. TOJ 3184 Mine sweeping
  5. 记一次坑爹的jconsole使用
  6. Aaja.pro 未定义
  7. tomcat8.5.8遇到的两个问题
  8. File 类 的基本操作
  9. Spring MVC+Fastjson之时间类型序列化
  10. 关于controller和apicontroller的跨域实现过滤器的不同