Linux多线程实例练习 - pthread_cancel()
Linux多线程实例练习 - pthread_cancel
1、代码 xx_pthread_cancel.c
#include <pthread.h>
#include <stdio.h>
#include <unistd.h> #define debug_Msg(fmt, arg...)\
do{\
printf("%s %d : ", __FILE__, __LINE__);\
printf(fmt, ##arg);\
}while() #define ENABLE_X
char * pe = "enable return";
void * state_Enable(void *arg)
{
int i = ;
int iExit = ;
while(i < && iExit == )
{
debug_Msg("state Enable : [%d]\n", i);
i++;
sleep();
}
char * p = pe;
return p;
} #define DISABLE_X
char * pd = "disable return";
void * state_Disable(void * arg)
{
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
int i = ;
int iExit = ;
while(i < && iExit == )
{
debug_Msg("state Disable : [%d]\n", i);
i++;
sleep();
}
char * p = pd;
return p;
} int main()
{
#ifdef ENABLE_X
pthread_t pid;
pthread_create(&pid, NULL, state_Enable, NULL);
sleep();
pthread_cancel(pid);
void * p = NULL;
printf("init with : [%08X]\n", (unsigned int)p);
pthread_join(pid, &p);
printf("pe addr : [%08X]\n", (unsigned int)pe);
printf("over with : [%08X]\n", (unsigned int)p);
#endif #ifdef DISABLE_X
pthread_t pDis;
pthread_create(&pDis, NULL, state_Disable, NULL);
sleep();
pthread_cancel(pDis);
p = NULL;
printf("init with : [%08X]\n", (unsigned int)p);
pthread_join(pDis, &p);
printf("pd addr : [%08X]\n", (unsigned int)pd);
printf("over with : [%08X]\n", (unsigned int)p);
#endif
}
2、CentOS 编译通过
g++ -g -c -o xx_pthread_cancel.o xx_pthread_cancel.c
g++ -g -o xx_pthread_cancel xx_pthread_cancel.o -lpthread
3、运行结果
$ ./xx_pthread_cancel
xx_pthread_cancel.c : state Enable : []
xx_pthread_cancel.c : state Enable : []
xx_pthread_cancel.c : state Enable : []
init with : []
pe addr : []
over with : [FFFFFFFF]
xx_pthread_cancel.c : state Disable : []
xx_pthread_cancel.c : state Disable : []
xx_pthread_cancel.c : state Disable : []
init with : []
xx_pthread_cancel.c : state Disable : []
xx_pthread_cancel.c : state Disable : []
xx_pthread_cancel.c : state Disable : []
xx_pthread_cancel.c : state Disable : []
xx_pthread_cancel.c : state Disable : []
xx_pthread_cancel.c : state Disable : []
xx_pthread_cancel.c : state Disable : []
pd addr : []
over with : []
Linux多线程实例练习 - pthread_cancel()的更多相关文章
- Linux多线程实例练习 - pthread_exit() 与 pthread_join()
Linux多线程实例练习 - pthread_exit 与 pthread_join pthread_exit():终止当前线程 void pthread_exit(void* retval); pt ...
- Linux多线程实例练习 - pthread_create()
Linux多线程实例练习 pthread_create():创建一个线程 int pthread_create(pthread_t *tidp, const pthread_attr_t *attr, ...
- Linux多线程实例解析
Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...
- Linux多线程实例 定时重启httpd和mysqld
#include <stdio.h> #include <pthread.h> void *start_routine(void *arg) { while(1) { syst ...
- Linux多线程编程实例解析
Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...
- 笔记整理--Linux多线程
Unix高级环境编程系列笔记 (2013/11/17 14:26:38) Unix高级环境编程系列笔记 出处信息 通过这篇文字,您将能够解答如下问题: 如何来标识一个线程? 如何创建一个新线程? 如何 ...
- 【操作系统作业-lab4】 linux 多线程编程和调度器
linux多线程编程 参考:https://blog.csdn.net/weibo1230123/article/details/81410241 https://blog.csdn.net/skyr ...
- linux多线程同步pthread_cond_XXX条件变量的理解
在linux多线程编程中,线程的执行顺序是不可预知的,但是有时候由于某些需求,需要多个线程在启动时按照一定的顺序执行,虽然可以使用一些比较简陋的做法,例如:如果有3个线程 ABC,要求执行顺序是A-- ...
- Linux 多线程应用中如何编写安全的信号处理函数
http://blog.163.com/he_junwei/blog/static/1979376462014021105242552/ http://www.ibm.com/developerwor ...
随机推荐
- Juery Ajax语法
$.ajax({ url: "/ForgetCard/ForgetLogin",//方法路径URL data: { strUser: $("#textUser" ...
- 在WPF中使用CefSharp嵌入浏览器
日常开发中,我们需要将一些Web页面嵌入到桌面客户端软件中.下面我们使用CefSharp嵌入浏览器来实现. 首先先介绍一下CefSharp嵌入式浏览器,它是基于Google浏览器的一个组件,我们可以在 ...
- 网站Session 处理方式
分布式session有以下几种方案: 1. 基于nfs(net filesystem)的session共享 将共享服务器目录mount各服务器的本地session目录,session读写受共享服务器i ...
- 字幕文件 WebVTT 与 srt 之间的互相转化
1. WebVTT 2 srt 1. 用记事本打开 .vtt 文件: 2. 在记事本中点击 编辑 -> 替换 -> 查找内容中输入".",替换为中输入",& ...
- 解决"System.AccessViolationException”类型的未经处理的异常在 未知模块(IIS Worker Process 已停止工作)导致无法连接远程数据库的问题
解决方法: 用管理员身份运行CMD,输入netsh winsock reset并回车(注意,必须是已管理员身份运行,这个重置LSP连接)
- NSDate用法整理总结
int main(int argc, const char * argv[]) { @autoreleasepool { NSDate *date=[NSDate date]; NSLog(@&quo ...
- java的分层开发
既然是分层开发,首先我们需要知道的是分为那几个层,并且是干什么的? 1.实体层(entity) 对应数据库中的一张表,有了它可以降低耦合性,同时也是数据的载体. 2.数据访问对象(data acces ...
- codeforces733D. Kostya the Sculptor 偏序cmp排序,数据结构hash,代码简化
对于n==100.1,1,2或者1,2,2大量重复的形状相同的数据,cmp函数最后一项如果表达式带等于,整个程序就会崩溃 还没有仔细分析std::sort的调用过程,所以这里不是很懂..,mark以后 ...
- 【pom.xml 依赖】使用net.sf.json-lib-2.4-jdk15.jar所需要的其他依赖架包 以及其一直在pom.xml报错的问题
特此声明: json-lib-2.4-jdk15.jar仅它本身不够,必须如下的几个依赖架包都有才能使用!!! 首先 将.json-lib-2.4-jdk15.jar以及其相关的依赖架包的正确配置给出 ...
- AchartEngine 的学习
第一步:我使用的事AchartEngine 1.1.0 的包.大家要先下在这个包,放到项目中,创建一个lib文件夹.然后倒金项目中去.然后再AndroidManifest.xml 中需要注册一下代码是 ...