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. [译]:Orchard入门——安装Orchard

    原文链接:Installing Orchard 文章内容基于Orchard 1.8版本 安装Orchard的方式 主要有以下四种方式安装Orchard: 利用Microsoft Web Platfor ...

  2. 【转】Linux 查看内存(free buffer cache)

    转自:http://elf8848.iteye.com/blog/1995638 Linux下如何查内存信息,如内存总量.已使用量.可使用量.经常使用Windows操作系统的朋友,已经习惯了如果空闲的 ...

  3. 改变图片尺寸(python)

    for name in /图片路径; do convert -resize 256x256! $name $namedone

  4. 那些Android中的性能优化

    性能优化是一个大的范畴,如果有人问你在Android中如何做性能优化的,也许都不知道从哪开始说起. 首先要明白的是,为什么我们的App需要优化,最显而易见的时刻:用户say,什么狗屎,刷这么久都没反应 ...

  5. [转] FastMM、FastCode、FastMove的使用

    http://blog.csdn.net/akof1314/article/details/6524767 FastMM是一个替换Embarcadero Delphi Win32应用程序的快速内存管理 ...

  6. 【转】win8.1下安装ubuntu

    参考:http://jingyan.baidu.com/article/14bd256e0ca52ebb6d26129c.html Ubuntu 系统是一款优秀的.基于GNU/Linux 的平台的桌面 ...

  7. Mvc网站发布到IIS

    网站发布步骤: 这部分是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因. 如有冒犯请联系本人,或删除,或标明出处. 因为好的文章,以前只想收藏,但连接 ...

  8. Silverlight

    http://kb.cnblogs.com/zt/silverlight/ http://www.cnblogs.com/gnielee/archive/2010/01/15/silverlight- ...

  9. ural 1073. Square Country

    1073. Square Country Time limit: 1.0 secondMemory limit: 64 MB There live square people in a square ...

  10. void和void*

    void的含义 void即“无类型”,void *则为“无类型指针”,可以指向任何数据类型. void指针使用规范①void指针可以指向任意类型的数据,亦即可用任意数据类型的指针对void指针赋值.例 ...