C++11 pthread_create:将参数作为最后一个参数传递
我有以下职能:C++11 pthread_create:将参数作为最后一个参数传递,c++11,pthreads,C++11,Pthreads,我有以下职能: 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>\n\n"; if ( SSL_accept(ssl
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>\n\n";
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 */
}
但在编译之后,我看到参数3 pthread_create的错误!如何修复它?如果您查看
pthread\u create
的声明,您会发现第三个参数(回调)的类型为void*(*start\u routine)(void*)
。此类函数指针可以指向类型为void*(void*)
的函数,即返回void*
并接受类型为void*
的参数的函数
函数Servlet
接受类型为SSL*
而不是void*
的参数,并返回void
而不是void*
。因此,您的函数不能转换为其他函数指针类型,因此对pthread\u create
的调用格式不正确
解决方案:使用具有正确签名的函数。使用void*
作为参数,并返回void*
另一种方法:C++代码库中使用<代码> STD::线程< /> >代替pTr.< /P> < P>我自己回答:
对于调用pthread_create:void * Servlet(void *param)
{
SSL *ssl = (SSL *)param;
//..
}
在启动程序主体中:
“我看到参数3 pthread_create出现错误!如何修复它”。第一步:阅读错误消息。错误消息“从void()(ssl)到void*()(void)[-fpermissive]的会话无效”。您应该在question.tnx中输入相关信息以获得答复。
pthread_create(&threadA[noThread], NULL, Servlet,(void*) ssl);
void * Servlet(void *param)
{
SSL *ssl = (SSL *)param;
//..
}