Chinaunix

标题: AIX下多线程程序代码出错请教 [打印本页]

作者: JimmyWuMr    时间: 2005-10-21 14:56
标题: AIX下多线程程序代码出错请教
从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);
}
作者: JimmyWuMr    时间: 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)




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2