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