2013-11-23 166 views
-2

我使用LINUX 10.04我认为这不是问题,无论如何,我有一个 奇怪的错误。 对我来说,所有看起来都很完美。 那么问题是什么? 对不起,这种格式type.i新来这里。使用Pthreads奇怪的编译错误

//COMPILE with: gcc -g -Wall -pthread pthread_ex_book_pg193.c -lpthread -o MYthread 

/* the program is simple.We create two threads One is for the main() and th esecond with the pthread_create(). 
The second thread calls a function runner() to calculate a sum and when it finishes it returns to the main thread */ 


#include <stdio.h> 
#include <pthread.h> 
#include <stdlib.h> 


int sum; 
void *runner(void *argv[]); 
int main (int argc,char *argv[]){ 
pthread_t tid; //thread id 
pthread_attr_t attr;//thread attributes 
if (argc!=2){ 
fprintf(stderr,"usage: a.out<integer value> \n"); 
return -1; 
} 
if (atoi(argv[1]) < 0){ 
fprintf(stderr, "%d must be >=0\n ",atoi(argv[1])); 
return -1; 
} 
pthread_attr_init(&attr); 
pthread_create(&tid,&attr,runner,argv[1]); 
pthread_join(tid,NULL); 
printf("sum = %d \n",sum); 
} 

void *runner(void *param){ 
int i; 
int upper = atoi(param); 
sum=0; 
for (i=1;i<=upper;i++){ 
sum=sum+i; 
pthread_exit(0); 
} 
} 
+2

编译器错误的确切文本是什么?猜测,这是关于你的“跑步者”的前向宣言吗?它应该是'void * runner(void * arg);' – simonc

+1

SO不是代码评论网站。试着制定一个关于你不明白的具体问题。错误消息很重要,它们提供信息,读取它们,分享它们。 –

+0

GCC打印的: pthread_ex_book_pg193.c:在函数 '主': pthread_ex_book_pg193.c:25:警告:从兼容的指针类型 /usr/include/pthread.h:225传递 '在pthread_create' 的参数3:注:期望'void *(*)(void *)',但参数的类型为'void *(*)(void **)' pthread_ex_book_pg193.c:顶级: pthread_ex_book_pg193.c:30:错误:冲突'runner'的类型 pthread_ex_book_pg193.c:12:note:之前的'runner'声明在这里 nkbaroutis @ nkbaroutis-VGN-FW11M:〜/ Cscripts/threads $ – TraffyT

回答

0

变化

void *runner(void *argv[]); 

void *runner(void *argv); 

(1)您与您以后使用亚军冲突的向前声明; (2)线程启动函数的正确签名是void* f(void*) - IOW它只需要一个指针就可以使指向void的指针数组无效。