gpt4 book ai didi

c - 指针和多线程中的错误预期为 ‘void * (*)(void *)’ 但参数的类型为 ‘pthread_t’

转载 作者:太空宇宙 更新时间:2023-11-04 01:27:54 25 4
gpt4 key购买 nike

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

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();对不起,我的英语不好

最佳答案

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* 并接受 1 个 void* 类型的参数),该函数将接收一个指针,您基本上必须将其转换回调用时传入的指针类型 pthread_create

关于c - 指针和多线程中的错误预期为 ‘void * (*)(void *)’ 但参数的类型为 ‘pthread_t’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28018045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com