pthread_create:传递参数作为最后一个参数



我有以下功能:

void Servlet(SSL* ssl)  /* Serve the connection -- threadable */
{   char buf[1024];
char reply[1024];
int sd, bytes;
const char* HTMLecho="<html><body><pre>%s</pre></body></html>nn";
if ( SSL_accept(ssl) == FAIL )          /* do SSL-protocol accept */
ERR_print_errors_fp(stderr);
else
{
ShowCerts(ssl);                             /* get any certificates */
bytes = SSL_read(ssl, buf, sizeof(buf));    /* get request */
if ( bytes > 0 )
{
buf[bytes] = 0;
printf("Client msg: "%s"n", buf);
sprintf(reply, HTMLecho, buf);          /* construct reply */
SSL_write(ssl, reply, strlen(reply));   /* send reply */
}
else
ERR_print_errors_fp(stderr);
}
sd = SSL_get_fd(ssl);           /* get socket    connection */
SSL_free(ssl);                                  /* release SSL state */
close(sd);                                      /* close connection */
}

在主要部分程序中:

pthread_create(&threadA[noThread], NULL, Servlet,(SSL*)(ssl));

但是编译后,我看到参数 3 pthread_create的错误! 如何解决?

如果你看一下pthread_create的声明,你会发现第三个参数(回调(是void *(*start_routine) (void *)的类型。这样的函数指针可以指向类型为void*(void*)的函数,即返回void*并采用类型void*参数的函数。

您的函数Servlet接受类型为SSL*而不是void*的参数,并返回void而不是void*。因此,您的函数不能转换该其他函数指针类型,因此您对pthread_create的调用格式不正确。

解决方案:使用具有正确签名的函数。使用void*作为参数,并返回一个void*

另一种方法:使用 C++ 标准库中的std::thread而不是 pthreads。

我自己回答:

对于呼叫pthread_create:

pthread_create(&threadA[noThread], NULL, Servlet,(void*) ssl);

并在启动例程的正文中:

void * Servlet(void *param) 
{
SSL *ssl = (SSL *)param;
//..
}

相关内容

最新更新