免费注册 查看新帖 |

Chinaunix

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

apue程序清单11-4用g++和gcc分别编译后输出结果不一致的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-01-11 11:39 |只看该作者 |倒序浏览
apue第二版P296程序清单11-4,代码在最下方。

将这段代码分别写在11-4.c和11-4.cpp中后
gcc 11-4.c -o prg1 -pthread
g++ 11-4.cpp -o prg2 -pthread

执行./prg1的输出是
thread 1 start
thread 1 push complete
thread 1 exit code 1
thread 2 start
thread 2 push complete
cleanup: thread 2 second handler
cleanup: thread 2 first handler
thread 2 exit code 2
与书中阐述的,“线程如果是通过启动例程返回而终止后,清理处理程序不会被调用”,这个说法一致

而执行./prg2的输出是
thread 1 start
thread 1 push complete
cleanup: thread 1 second handler
cleanup: thread 1 first handler
thread 1 exit code 1
thread 2 start
thread 2 push complete
cleanup: thread 2 second handler
cleanup: thread 2 first handler
thread 2 exit code 2
无论是线程调用pthread_exit退出或者是从启动例程返回,都执行了清理处理程序。

想请教下,造成这结果的原因是什么??我试过生成汇编文件后对比,有不少地方是不一致的,
不过小菜汇编实在菜,看不懂。。求助下~
#include "apue.h"
#include <pthread.h>

void cleanup(void *arg) {
    printf("cleanup: %s\n", (char *)arg);
}

void *thr_fn1(void *arg) {
    printf("thread 1 start\n");
    char str1[] = "thread 1 first handler";
    char str2[] = "thread 1 second handler";
    pthread_cleanup_push(cleanup, str1);
    pthread_cleanup_push(cleanup, str2);
    printf("thread 1 push complete\n");
    if (arg)
        return ((void *)1);
    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);
    return ((void *)1);
}

void *thr_fn2(void *arg) {
    printf("thread 2 start\n");
    char str1[] = "thread 2 first handler";
    char str2[] = "thread 2 second handler";
    pthread_cleanup_push(cleanup, str1);
    pthread_cleanup_push(cleanup, str2);
    printf("thread 2 push complete\n");
    if (arg)
        pthread_exit((void *)2);
    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);
    pthread_exit((void *)2);
}

int main() {
    pthread_t tid1, tid2;
    void *tret;

    pthread_create(&tid1, NULL, thr_fn1, (void *)1);
    sleep(1);
    pthread_create(&tid2, NULL, thr_fn2, (void *)2);

    pthread_join(tid1, &tret);
    printf("thread 1 exit code %d\n", (int)tret);

    pthread_join(tid2, &tret);
    printf("thread 2 exit code %d\n", (int)tret);
    exit(0);
}

论坛徽章:
1
射手座
日期:2014-08-04 16:49:43
2 [报告]
发表于 2012-01-11 12:08 |只看该作者
g++和gcc 同样的C程序编译出来结果不同很正常,G++优化了很多, pthread_create在类成员里使用的时候 还会有void *的错误  很麻烦的.....

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
3 [报告]
发表于 2012-01-11 12:11 |只看该作者
回复 1# firefox_hy


    C 的程序为什么要用 C++ 编译器来编译?

论坛徽章:
0
4 [报告]
发表于 2012-01-11 13:43 |只看该作者
哦哦,谢谢2楼了:)
回3楼,一直习惯用g++编译,以后会视情况选择的了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP