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()的更多相关文章

  1. Linux多线程实例练习 - pthread_exit() 与 pthread_join()

    Linux多线程实例练习 - pthread_exit 与 pthread_join pthread_exit():终止当前线程 void pthread_exit(void* retval); pt ...

  2. Linux多线程实例练习 - pthread_create()

    Linux多线程实例练习 pthread_create():创建一个线程 int pthread_create(pthread_t *tidp, const pthread_attr_t *attr, ...

  3. Linux多线程实例解析

    Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...

  4. Linux多线程实例 定时重启httpd和mysqld

    #include <stdio.h> #include <pthread.h> void *start_routine(void *arg) { while(1) { syst ...

  5. Linux多线程编程实例解析

    Linux系统下的多线程遵循POSIX线程接口,称为 pthread.编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a.顺便说一下,Linux ...

  6. 笔记整理--Linux多线程

    Unix高级环境编程系列笔记 (2013/11/17 14:26:38) Unix高级环境编程系列笔记 出处信息 通过这篇文字,您将能够解答如下问题: 如何来标识一个线程? 如何创建一个新线程? 如何 ...

  7. 【操作系统作业-lab4】 linux 多线程编程和调度器

    linux多线程编程 参考:https://blog.csdn.net/weibo1230123/article/details/81410241 https://blog.csdn.net/skyr ...

  8. linux多线程同步pthread_cond_XXX条件变量的理解

    在linux多线程编程中,线程的执行顺序是不可预知的,但是有时候由于某些需求,需要多个线程在启动时按照一定的顺序执行,虽然可以使用一些比较简陋的做法,例如:如果有3个线程 ABC,要求执行顺序是A-- ...

  9. Linux 多线程应用中如何编写安全的信号处理函数

    http://blog.163.com/he_junwei/blog/static/1979376462014021105242552/ http://www.ibm.com/developerwor ...

随机推荐

  1. HDU2546 饭卡(背包)

    开始写成01背包的形式,求m元可买物品价值的最大值 dp[j] = max(dp[j], dp[j - pri[i]] + pri[i]) 结果为m - dp[m] 但后来发现是有问题的, 比如这组过 ...

  2. IBM AppScan 安全扫描:支持弱 SSL 密码套件 分类: 数据安全 2014-06-28 11:34 1844人阅读 评论(0) 收藏

    问题描述: ​ 解决方法: 1.Server 2008(R2) 根据appScan的修订建议访问地址:http://msdn.microsoft.com/en-us/library/windows/d ...

  3. 5-04用Sql语句创建表

    用Sql语句创建表的基本语法: USE E_Market--指向当前所操作的数据库 GO CREATE TABLE CommoditySort--创建表的名字 { sortID int IDENTIT ...

  4. apt-get常见错误——Unmet dependencies

    转自:http://blog.sina.com.cn/s/blog_4980828b0100zicn.html 安装错误:“E: Unmet dependencies.”原因:非正常停止apt-get ...

  5. 国内大学毕业论文 LaTeX 模板集合

    西北工业大学学位论文LaTeX模板 http://code.google.com/p/nwputhesis/ 西北工业大学硕博士论文LaTeX模版 http://code.google.com/p/n ...

  6. Pushlet浏览器长连接通讯

    原文链接:http://cuisuqiang.iteye.com/blog/1416771 Pushlet(一种comet 架构的实现)是基于Servlet 机制,数据从server端的Java 对象 ...

  7. HTML5_ScrollInToView方法

    scrollIntoView(ture)元素上边框与视窗顶部齐平 scrollIntoView(false)元素下边框与视窗底部齐平 <html> <head> <tit ...

  8. Android 自定义title 之Action Bar

    Android 自定义title 之Action Bar 2014-06-29  飞鹰飞龙...  摘自 博客园  阅 10519  转 25 转藏到我的图书馆   微信分享:   Action Ba ...

  9. Loadrunner中参数化实战(9)-Unique+Once

    参数化数据30条: 脚本如下,演示登录,投资,退出操作是,打印手机号: 首先验证Vugen中迭代: Unique+Once 设置迭代4次Action 结果如下:

  10. 廖雪峰js教程笔记7 基本类型和包装类型

    在JavaScript的世界里,一切都是对象. 但是某些对象还是和其他对象不太一样.为了区分对象的类型,我们用typeof操作符获取对象的类型,它总是返回一个字符串: typeof 123; // ' ...