我从 https://computing.llnl.gov/tutorials/pthreads/ 网站上找到了下面这个演示。
这对我来说毫无意义,因为头文件包含了
#include <pthread.h>
#include <stdio.h>
#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<NUM_THREADS; 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);
}
但是当我在我的机器上编译它(运行Ubuntu Linux 9.04)时,我会得到以下错误:
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
这对我来说毫无意义,因为头文件包含了
pthread.h
,应该有pthread_create
函数。你知道出了什么问题吗?
-lc_r
),(c)使用-thread
、-threads
或其他选项,而不是或除了-lpthread
。 - ephemient-lpthread
。 - How ChenLDFLAGS= -pthread -lpthread
。该指令将添加-pthread
和-lpthread
标志到链接器标志中,以在编译过程中启用线程支持。 - dsnkpthread_exit(NULL);
是必须的/良好的实践吗? - Vikram