我必须将“risposta”类型的参数“r”发送到函数 RispostaServer。编译器给我:invalid conversion void*(*)() to void*(*)(void*)
这是我应该更正的代码片段:
{/*other istructions*/
risposta r;
r.mess = m1;
r.codaSC = codaSC;
pthread_create(&threads[threads_index],&attr,RispostaServer,(void*)&r);
threads_index++;
}
void* RispostaServer(void* m){
risposta* m1 = (risposta*) m;
/*other istructions*/
}
我应该编辑什么?我正在按小时尝试。
最佳答案
在 [MCVE] 之前,我会在黑暗中尝试一下(但请提供一个)。
RispostaServer
的声明看起来像这样吗?
void* RispostaServer();
那么 pthread_create
调用可见的唯一版本的 RispostaServer
是不接受参数的版本。这与编译器踢出的类型投诉相匹配。
你后面的函数 定义 创建了一个 RispostaServer
的新重载,确实接受一个参数,你可以调用它关闭代码,但到那时调用 pthread_create
已经太晚了。
声明应符合定义:
// Entrypoint for Risposta worker thread.
// Argument must be a risposta*, cast to `void*`.
void* RispostaServer(void* m);
顺便说一句,你的线程会被破坏,因为你传递了一个指向立即超出范围的局部变量的指针,所以在上面添加以下注释:
// The risposta it points to must exist for the lifetime
// of the thread.
……你真的应该使用 std::thread
而不是平台特定库的 C API。
关于C++ PTHREADS - 无效的转换 void*(*)() 到 void*(*)(void*),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52890335/