指针和多线程中的错误预期为“void * (*)(void *)”,但参数类型为“pthread_t”
error in pointer & multithread expected ‘void * (*)(void *)’ but argument is of type ‘pthread_t’
此代码使用多线程检查数独。
当我 运行 程序编译后:
分段错误(核心已转储)
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’
我的函数是给 0 个参数 :void *sub();
对不起,我的英语不好
pthread_create
的原型是这个
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
在此,第三个参数必须是一个返回空指针的函数,它接受一个参数空指针。
所以像上面那样创建函数,然后将其传递给 pthread_create 的第三个参数。
void * thr_fn2(void *arg)// your function must be like this.
然后将该函数用作参数。
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;
}
您需要将一个函数作为第三个参数传递(returns void* 并接受 1 个 void* 类型的参数),该函数将接收一个指针,您基本上必须将其转换回指针类型您在调用 pthread_create
时传入
此代码使用多线程检查数独。 当我 运行 程序编译后: 分段错误(核心已转储)
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’
我的函数是给 0 个参数 :void *sub(); 对不起,我的英语不好
pthread_create
的原型是这个
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg);
在此,第三个参数必须是一个返回空指针的函数,它接受一个参数空指针。
所以像上面那样创建函数,然后将其传递给 pthread_create 的第三个参数。
void * thr_fn2(void *arg)// your function must be like this.
然后将该函数用作参数。
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;
}
您需要将一个函数作为第三个参数传递(returns void* 并接受 1 个 void* 类型的参数),该函数将接收一个指针,您基本上必须将其转换回指针类型您在调用 pthread_create