免费注册 查看新帖 |

Chinaunix

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

[C] pthread_exit 参数的不正确使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-03-09 16:26 |只看该作者 |倒序浏览
本人新手,在学习APUE,遇到下面的问题,哪位朋友能帮帮忙解决下
注:在ubuntu环境下

#include "apue.h"
#include <pthread.h>

struct foo {
        int a, b, c, d;
};

void
printfoo(const char *s, const struct foo *fp)
{
        printf(s);
        printf("  structure at 0x%x\n", (unsigned)fp);
        printf("  foo.a = %d\n", fp->a);
        printf("  foo.b = %d\n", fp->b);
        printf("  foo.c = %d\n", fp->c);
        printf("  foo.d = %d\n", fp->d);
}

void *
thr_fn1(void *arg)
{
        struct foo        foo = {1, 2, 3, 4};

        printfoo("thread 1:\n", &foo);
        pthread_exit((void *)&foo);
}

void *
thr_fn2(void *arg)
{
        printf("thread 2: ID is %d\n", pthread_self());
        pthread_exit((void *)0);
}

int
main(void)
{
        int                        err;
        pthread_t        tid1, tid2;
        struct foo        *fp;

        err = pthread_create(&tid1, NULL, thr_fn1, NULL);
        if (err != 0)
                err_quit("can't create thread 1: %s\n", strerror(err));
        err = pthread_join(tid1, (void *)&fp);
        if (err != 0)
                err_quit("can't join with thread 1: %s\n", strerror(err));
        sleep(1);
        printf("parent starting second thread\n";
        err = pthread_create(&tid2, NULL, thr_fn2, NULL);
        if (err != 0)
                err_quit("can't create thread 2: %s\n", strerror(err));
        sleep(1);
        printfoo("parent:\n", fp);
        exit(0);
}
编译没通过,不知为啥,哪位朋友帮忙解决下
编译的结果:fay@ubuntu:~/APUE/programming/11-3$ gcc 111-3.c error.c -o 111-3
/tmp/cceOIH2c.o: In function `main':
111-3.c.text+0x67): undefined reference to `pthread_create'
111-3.c.text+0xae): undefined reference to `pthread_create'
111-3.c.text+0xe4): undefined reference to `pthread_join'
111-3.c.text+0x12d): undefined reference to `pthread_join'
collect2: ld 返回 1
很郁闷为什么会出现上面的结果,还有,请帮帮忙解释一下pthread_exit((void *)&foo)和pthread_join(tid1, (void *)&fp)参数到底是怎么回事阿?

论坛徽章:
0
2 [报告]
发表于 2010-03-09 16:55 |只看该作者
包含线程的函数在编译的时候需要加上-lpthread选项吧

论坛徽章:
0
3 [报告]
发表于 2010-03-09 17:47 |只看该作者
包含线程的函数在编译的时候需要加上-lpthread选项吧
empty141 发表于 2010-03-09 16:55


正解

论坛徽章:
0
4 [报告]
发表于 2010-03-09 18:28 |只看该作者
回复 2# empty141


    昂~~呵呵~谢谢,调试通了

论坛徽章:
0
5 [报告]
发表于 2010-03-09 18:28 |只看该作者
回复 3# xti9er


    恩~确实是这样子 阿~谢谢阿

论坛徽章:
0
6 [报告]
发表于 2010-03-09 21:24 |只看该作者
恩,初学啊,呵呵

论坛徽章:
0
7 [报告]
发表于 2010-03-10 08:42 |只看该作者
本人新手,在学习APUE,遇到下面的问题,哪位朋友能帮帮忙解决下
注:在ubuntu环境下

#include "apue.h ...
单眼皮大姐 发表于 2010-03-09 16:26


没引用pthread库。

程序有问题,线程的栈已经销毁了,但是主线程还在访问其中的数据。

论坛徽章:
0
8 [报告]
发表于 2010-03-11 18:45 |只看该作者
回复 7# huangwei0413


    这位朋友可以具体的讲讲吗?很不了解怎么就摧毁栈了,还有那函数指针传递问题,先谢谢啊

论坛徽章:
0
9 [报告]
发表于 2010-03-13 20:51 |只看该作者
-lpthread

论坛徽章:
0
10 [报告]
发表于 2010-03-13 23:35 |只看该作者
-lpthread
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP