免费注册 查看新帖 |

Chinaunix

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

[故障求助] AIX下多线程程序代码出错请教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-10-21 14:56 |只看该作者 |倒序浏览
从Stevens大师UNIX Network Programming, Volume 2 IPC 一书中拷贝了一个多线程的程序代码,编译出错如下:
        xlc -o ../prog/prodcons2 ../objs/prodcons2.o ../objs/error.o
ld: 0711-317 ERROR: Undefined symbol: .pthread_create
ld: 0711-317 ERROR: Undefined symbol: .pthread_join
ld: 0711-317 ERROR: Undefined symbol: .pthread_mutex_lock
ld: 0711-317 ERROR: Undefined symbol: .pthread_mutex_unlock
ld: 0711-317 ERROR: Undefined symbol: .pthread_mutex_unlocak
ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information.
make: 1254-004 The error code from the last command is 8.


Stop.

实际上pthread_create, pthread_join, pthread_mutex_lock这几个函数在头文件pthread.h中都已经有了,man pthread_create有如下提示:
Note: The pthread.h header file must be the first included file of each source file using the threads library. Otherwise, the -D_THREAD_SAFE  compilation flag should be used, or the cc_r compiler used. In this  case, the flag is automatically set.

故我在prodcons2.c中的第一行#include <pthread.h>;
但编译还是报线程操作的几个函数没有声名。
请教各位高手,这是什么原因?

附:源代码如下:
/* prodcons2.c */
#include <pthread.h>;
#include "unpipc.h"

#define        MAXNITEMS        1000000
#define MAXNTHREADS        100

int nitems;        /* read-only by producer and consumer */
struct{
        pthread_mutex_t mutex;
        int buff[MAXNITEMS];
        int nput;
        int nval;
} shared = {
        PTHREAD_MUTEX_INITIALIZER
};

void *produce(void *), *consume(void *);

int
main(int argc, char ** argv)
{
        int i, nthreads, count[MAXNTHREADS];
        pthread_t tid_produce[MAXNTHREADS], tid_consume;

        if(argc != 3)
                err_quit("usage: prodcons2 <#items>; <#threads>;";
        nitems = min(atoi(argv[1]), MAXNITEMS);
        nthreads = min(atoi(argv[2]), MAXNTHREADS);

        set_concurrency(nthreads);
                /* start all the producer threads */
        for(i = 0; i< nthreads; i++)
        {
                count=0;
                pthread_create(&tid_produce, NULL, produce, &count);
        }

        for(i=0;i<nthreads; i++)
        {
                pthread_join(tid_produce, NULL);
                printf("count[%d] = %d\n", i, count);
        }

        pthread_create(&tid_consume, NULL, consume, NULL);
        pthread_join(tid_consume, NULL);

        exit(0);
}

void *
produce(void *arg)
{
        for(;
        {
                pthread_mutex_lock(&shared.mutex);
                if(shared.nput >;= nitems){
                        pthread_mutex_unlock(&shared.mutex);
                        return(NULL);
                }
                shared.buff[shared.nput] = shared.nval;
                shared.nput++;
                shared.nval++;
                Pthread_mutex_unlocak(shared.mutex);
                *((int *)arg)+=1;
        }
}

void *
consume(void *arg)
{
        int i;
        for(i=0;i<nitems; i++)
        {
                if(shared.buff!= i)
                        printf("buff[%d]=%d\n", i, shared.buff);
        }
        return(NULL);
}

论坛徽章:
0
2 [报告]
发表于 2005-10-24 11:02 |只看该作者

AIX下多线程程序代码出错请教

在链接库加上libpthreads.a;
在编译选项加上-qthreaded
makefile 如下:
#variables definitions
#path
OBJPATH = ../objs
PROGPATH = ../prog
SRCPATH = ../src

#other variables
EXE = prodcons2

CC = xlc_r
CFLAGS = -c

# target programs
.Jimmy:all
all(EXE)

prodcons2:prodcons2.o error.o
        $(CC) -o $(PROGPATH)/prodcons2 /usr/lib/libpthreads.a $(OBJPATH)/prodcon
s2.o $(OBJPATH)/error.o -qthreaded
prodcons2.o:prodcons2.c unpipc.h
        $(CC) $(CFLAGS) $(SRCPATH)/prodcons2.c -o $(OBJPATH)
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP