首页 > 解决方案 > 与 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;
    }

  }
}

标签: clinuxmultithreadingoperating-systempthreads

解决方案


推荐阅读