免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1827 | 回复: 8
打印 上一主题 下一主题

[C] 看看,为什么没有设置取消点,程序还能取消?(已解决,谢谢) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-16 17:00 |只看该作者 |倒序浏览
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>


void *child1(void *arg)
{
        int i = 0;
        printf("come in thread_id=%u\n",pthread_self());

        pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
        pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,NULL);

        while(1){
                //printf("Iam comin while my pthread_id=%u\ti=%d\n",pthread_self(),i++);
                printf("i=%d\n",i++);
        }
        //return (void *)6;
}

int main( int argc, char *argv[] )
{
        pthread_t pthread1;
        int ret;

        pthread_create(&pthread1,NULL,(void *)child1,NULL);
        sleep(1);
       
        ret = pthread_cancel(pthread1);
        if (0 != ret){
                perror("call pthread_cancel");
                return -1;
        }

        ret = pthread_join(pthread1, NULL);
        if (0 != ret){
                perror("call pthread_join");
                return -1;
        }

        printf("over main!return\n");
        return 0;
}

在main函数中设置了pthread_cancel(),但在child1中没有设置取消点,为什么不会死循环?而是执行到一定数值就退出?

[ 本帖最后由 foxwolf_ym 于 2008-7-16 17:53 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-07-16 17:06 |只看该作者
IO也是取消点之一。

论坛徽章:
0
3 [报告]
发表于 2008-07-16 17:07 |只看该作者
原帖由 cugb_cat 于 2008-7-16 17:06 发表
IO也是取消点之一。


有没有什么资料看看呀。没有找到IO也是取消点呀。

论坛徽章:
0
4 [报告]
发表于 2008-07-16 17:13 |只看该作者
我从man pthread_cancel中查出:
Linux- Threads is not yet integrated enough with the C library to implement this, and thus none of the C library functions is a cancel-lation point.

C库的函数都不是取消点呀!printf也是C库里的呀?!

论坛徽章:
0
5 [报告]
发表于 2008-07-16 17:21 |只看该作者
原帖由 foxwolf_ym 于 2008-7-16 17:13 发表
我从man pthread_cancel中查出:
Linux- Threads is not yet integrated enough with the C library to implement this, and thus none of the C library functions is a cancel-lation point.

C库的函数都不 ...

printf调用write,write是取消点。

论坛徽章:
1
双子座
日期:2015-01-04 14:25:06
6 [报告]
发表于 2008-07-16 17:27 |只看该作者
APUE2里面有讲

论坛徽章:
0
7 [报告]
发表于 2008-07-16 17:35 |只看该作者
这么说pthread_cancel()这个函数不太好用了,即使程序中设置了pthread_testcancel(),都可能不知道程序是在什么地方取消的了。

论坛徽章:
0
8 [报告]
发表于 2008-07-16 17:53 |只看该作者
我执行了楼主的代码,似乎不会退出啊?楼主那边什么时候退出的?

论坛徽章:
0
9 [报告]
发表于 2008-07-17 10:33 |只看该作者
原帖由 william9 于 2008-7-16 17:53 发表
我执行了楼主的代码,似乎不会退出啊?楼主那边什么时候退出的?


会呀,大概执行一会就退出了!

我用的是linux系统。glibc2.7
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP