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

时间:2017-07-24 18:29:18

标签: 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) == 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的错误!如何解决?

2 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

自己回答:

用于调用pthread_create:

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

并且在开始例程中:

void * Servlet(void *param) 
{
  SSL *ssl = (SSL *)param;
  //..
}
相关问题