linux 'pthread_create'未定义的引用,c - Undefined reference to pthread_create in Linux - Stack Overflow...

I picked up the following demo off the web from https://computing.llnl.gov/tutorials/pthreads/

#include

#include

#define NUM_THREADS 5

void *PrintHello(void *threadid)

{

long tid;

tid = (long)threadid;

printf("Hello World! It's me, thread #%ld!\n", tid);

pthread_exit(NULL);

}

int main (int argc, char *argv[])

{

pthread_t threads[NUM_THREADS];

int rc;

long t;

for(t=0; t

printf("In main: creating thread %ld\n", t);

rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);

if (rc){

printf("ERROR; return code from pthread_create() is %d\n", rc);

exit(-1);

}

}

pthread_exit(NULL);

}

But when I compile it on my machine (running Ubuntu Linux 9.04) I get the following error:

corey@ubuntu:~/demo$ gcc -o term term.c

term.c: In function ‘main’:

term.c:23: warning: incompatible implicit declaration of built-in function ‘exit’

/tmp/cc8BMzwx.o: In function `main':

term.c:(.text+0x82): undefined reference to `pthread_create'

collect2: ld returned 1 exit status

This doesn't make any sense to me, because the header includes pthread.h, which should have the pthread_create function. Any ideas what's going wrong?

``` typedef struct { pthread_mutex_t hLock; volatile int iIsRun; void* (* volatile pfnTask)(void*); //函数指针 void *arg; //任务参数 }Task; typedef struct { pthread_mutex_t StackLock; int iIDStack[MAX_TASK_NUM]; int iTopPointer; }Stack; static Stack g_IDTaskStack; static Task stTaskQueueAry[MAX_TASK_NUM]; static int iTaskCount = 0; //static pthread_mutex_t stMutexQueue; //static pthread_cond_t stCondQueue; static pthread_t stThreadAry[cmnDfn_WORKING_PTHREAD_NUMBER]; void* PthreadPoolWorker(void* arg) { int iRet = -1; int iTskID = (int)(long)arg; Task *pstTsk = &stTaskQueueAry[iTskID]; while(g_iExit == 0) { if (pstTsk->iIsRun == 0) { sleep(1); continue; } iRet = (int)(long)pstTsk->pfnTask(pstTsk->arg); if(iRet < 0) { break; } if (iRet == 0) { continue; } LOCK(pstTsk->hLock) { pstTsk->pfnTask = NULL; pstTsk->iIsRun = 0; } LOCK(g_IDTaskStack.StackLock) { g_IDTaskStack.iIDStack[++g_IDTaskStack.iTopPointer] = iTskID; } } iRet = 0; _Exit: return (void *)(long)iRet; } int ThreadPoolSubmit(void* (*task)(void*),void* arg,int *piTskID) { int iLoopNum; int iTskID = 0; /*for(iLoopNum = 0; iLoopNum < MAX_TASK_NUM;iLoopNum++) { if (stTaskQueueAry[iLoopNum].pfnTask == NULL) { break; } } if(iLoopNum >= MAX_TASK_NUM) { return -1; }*/ if(g_IDTaskStack.iTopPointer < 0) { return -1; } LOCK(g_IDTaskStack.StackLock) { iTskID = g_IDTaskStack.iIDStack[g_IDTaskStack.iTopPointer--]; } LOCK(stTaskQueueAry[iTskID].hLock) { stTaskQueueAry[iTskID].pfnTask = task; stTaskQueueAry[iTskID].arg = arg; stTaskQueueAry[iTskID].iIsRun = 1; *piTskID = iTskID; } return 0; } int ThreadPoolInit(void) { int iLoopNum; int iRet = -1; int iTskID = 0; g_IDTaskStack.iTopPointer = -1; for(iLoopNum = 0; iLoopNum < MAX_TASK_NUM; iLoopNum++) { g_IDTaskStack.iIDStack[++g_IDTaskStack.iTopPointer] = iLoopNum; } for(iLoopNum = 0; iLoopNum < MAX_TASK_NUM; iLoopNum++) { if(pthread_mutex_init(&stTaskQueueAry[iLoopNum].hLock,NULL) < 0) { cmn_PrtError("Error in initializing mutex"); } if(pthread_create(&stThreadAry[iLoopNum],NULL,PthreadPoolWorker,(void *)(long)iTskID) != 0) { cmn_PrtError("Error in creating thread"); } stTaskQueueAry[iLoopNum].pfnTask = NULL; stTaskQueueAry[iLoopNum].iIsRun = 0; iTskID++; } iRet = 0; _Exit: return iRet; } int ThreadPoolDestroy(void) { int iLoopNum; int iRet = -1; g_iExit = 1; for(iLoopNum = 0; iLoopNum < MAX_TASK_NUM; iLoopNum++) { pthread_join(stThreadAry[iLoopNum], NULL); } for(iLoopNum = 0; iLoopNum < MAX_TASK_NUM; iLoopNum++) { pthread_mutex_destroy(&stTaskQueueAry[iLoopNum].hLock); } iRet = 0; return iRet; } #ifndef LOCK #define LOCK(mtx) \ for (int _locked = (pthread_mutex_lock(&(mtx)), 1); \ _locked; \ pthread_mutex_unlock(&(mtx)), _locked = 0) #endif #ifndef cmn_PrtError #define cmn_PrtError(str)\ printf("%s\n", str);\ goto _Exit; #endif```检查代码是否有bug,并给出修改方案
最新发布
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值