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. 用CocoaPods做iOS程序的依赖管理(转摘)

    转摘自:http://blog.devtang.com/blog/2014/05/25/use-cocoapod-to-manage-ios-lib-dependency/ 文档更新说明 2012-1 ...

  2. jq与js 区别

    $(this).html(666); <div id="a">123</div> <script> $("#a").clic ...

  3. WPF QuickStart系列之数据绑定(Data Binding)

    这篇博客将展示WPF DataBinding的内容. 首先看一下WPF Data Binding的概览, Binding Source可以是任意的CLR对象,或者XML文件等,Binding Targ ...

  4. 在ASP.NET 5中使用SignalR

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:SignalR作为ASP.NET中进行Web实时双向通信的组件,在ASP.NET 5中 ...

  5. C语言中main函数的参数

    转自:http://blog.csdn.net/cnctloveyu/article/details/3905720 我们经常用的main函数都是不带参数的.因此main 后的括号都是空括号.实际上, ...

  6. ARM指令学习,王明学learn

    ARM指令学习 一.算数和逻辑指令 1— MOV 数据传送指令    2.— MVN 数据取反传送指令    3.— CMP 比较指令    4.— CMN 反值比较指令    5.— TST 位测试 ...

  7. windows下mongodb安装与使用整理

    一.首先安装mongodb 1.下载地址:http://www.mongodb.org/downloads 2.解压缩到自己想要安装的目录,比如d:\mongodb 3.创建文件夹d:\mongodb ...

  8. Java ClassLoader基础及加载不同依赖 Jar 中的公共类

    转载自:最新内容及最清晰格式请见 http://www.trinea.cn/android/java-loader-common-class/ 本文主要介绍 ClassLoader 的基础知识,Cla ...

  9. poj2533 LIS

    题目链接: http://poj.org/problem?id=2533 题意:第一个数n,接下来n个数,>  ....求最长上升子序列. 这道题有两种解法,第一种是通解,也适用于别的LIS. ...

  10. sprint3冲刺第二天

    队友: 郭志豪:http://www.cnblogs.com/gzh13692021053/ 杨子健:http://www.cnblogs.com/yzj666/ 刘森松:http://www.cnb ...