c - 与 Linux 中接受 void 的线程函数相关的错误?
问题描述
我在 Linux 中收到错误消息:
在 server.c:7 中包含的文件中:/usr/include/pthread.h:200:15:注意:预期为 'void * ()(void *)' 但参数类型为 'void ()(int)' 200 | 无效*(__start_routine)(无效*),| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~ -</p>
我知道线程函数必须接受一个 void* 参数并返回一个 void*。接受任何其他参数都是错误的。返回任何其他结果都是错误的。我对如何格式化下面的代码以修复错误感到困惑。
这是导致错误的代码:
if(pthread_create( &thread_id , NULL , func , (void*) &connfd) < 0)
{
perror("could not create thread");
}
Here is the definition of func:
void func(int *sockfd)
{
char buff[MAX];
int n;
// infinite loop for chat
for (;;) {
bzero(buff, MAX);
// read the message from client and copy it in buffer
read(*sockfd, buff, sizeof(buff));
// print buffer which contains the client contents
printf("From client: %s\t To client : ", buff);
bzero(buff, MAX);
n = 0;
// copy server message in the buffer
while ((buff[n++] = getchar()) != '\n');
// and send that buffer to client
write(*sockfd, buff, sizeof(buff));
// if msg contains "Exit" then server exit and chat ended.
if (strncmp("exit", buff, 4) == 0) {
printf("Server Exit...\n");
break;
}
}
}
解决方案
推荐阅读
- maven - 如何在 Maven 中按名称包含/排除 JUnit 5 `ParameterizedTest`
- ios - 如何快速填充图案图像的颜色?
- css - 内联动画样式在 Reactjs 中不起作用
- python - 基于范围列表过滤数据框
- swift - 和 (&&) 迅速的逻辑
- node.js - 如何摆脱“TypeError:req.pipe 不是函数”nestjs 和 fastify
- javascript - 做出反应。将数据从 textarea 传输到数组 JSON
- ios - 如何在 RxSwift 中使用 flatMap 观察上层 Observable?
- javascript - MobX 状态树异步操作和重新渲染 React 组件
- jquery - 知道如何在其他圈子上添加一些延迟