指针错误多线程预期'void *(*)(void *)'但参数的类型为'pthread_t'

时间:2015-01-19 04:38:56

标签: c multithreading

此代码使用多线程检查数独。 当我在编译后运行程序时: 分段错误(核心转储)

int main(){
char t0,t1,t2;

pthread_t row,col,sub1;

t0=pthread_create(&row,NULL,row,NULL); //eror iz here!
t1=pthread_create(&col,NULL,col,NULL);
t2=pthread_create(&sub1,NULL,sub,NULL);

pthread_join(row, NULL);
pthread_join(col, NULL);
pthread_join(sub1, NULL);

exit(EXIT_SUCCESS);
return 0;
}

和错误:

su.c: In function ‘main’:
su.c:87:2: warning: passing argument 3 of ‘pthread_create’ makes pointer from integer without a cast [enabled by default]
In file included from su.c:4:0:
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘pthread_t’
su.c:88:2: warning: passing argument 3 of ‘pthread_create’ makes pointer from integer without a cast [enabled by default]
In file included from su.c:4:0:
/usr/include/pthread.h:225:12: note: expected ‘void * (*)(void *)’ but argument is of type ‘pthread_t’

我的func是给0参数:void * sub(); 对不起,我的英语不好

2 个答案:

答案 0 :(得分:2)

pthread_create的原型就是这个。

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                      void *(*start_routine) (void *), void *arg);

在这里,第三个参数必须是一个返回void指针的函数,它接受一个参数void指针。

所以使上面的函数然后传递给pthread_create的第三个参数。

void * thr_fn2(void *arg)// your function must be like this.

然后使用该函数作为参数。

答案 1 :(得分:2)

void * my_row_function(void *param){ 
    Row * myrow = (Row*) param;
    //bla bla
}

int main(){
    Row * a_row= & row8outof9;
    pthread_create(&row,(const pthread_t*)NULL,
             my_row_function, a_row);
    return 0;
}

你需要传递一个函数作为第三个参数(返回void *并接受类型为void *的1个参数),该函数将接收一个指针,你基本上必须强制转换回调用时传入的指针类型{ {1}}

相关问题