c - 使用Pthreads产生奇怪的编译错误

标签 c multithreading pthreads posix

我正在使用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/

相关文章:

c++ - 如何使用 std::threads 将多维映射的引用传递给函数

c - pthread 在线程函数中出现障碍后在连接时停止

c++ - 调用 pthread_create() 时出现段错误 C++ Linux

c - 在 C99 中将运算符作为参数传递

无法将 int 分配给 C 中取消引用的 int

java - 通过 java 向 Minecraft 服务器发送命令

c - 只阻塞消费者的单一生产者/消费者循环缓冲区

c - C 中 INT_MAX 宏和未初始化值的使用

c - 如何在 Windows 中使用 Ctrl+D 结束一个 c 程序?

C++ vector/类/线程