我正在使用LINUX 10.04,我认为这不是问题,无论如何我都有
奇怪的错误。
在我看来,一切都很完美。
那是什么问题呢?
抱歉,这种格式类型。我是新来的。
//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);
}
}
最佳答案
更改
void *runner(void *argv[]);
至
void *runner(void *argv);
(1)您对赛跑者的前期声明与您以后的使用方式相冲突; (2)线程的启动函数的正确签名是
void* f(void*)
-IOW,它只需要一个指向void的指针,而不是指向void的指针数组。
关于c - 使用Pthreads产生奇怪的编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20164931/