2017-07-24 110 views
-1

我有以下功能:在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>\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!如何解决它?

+0

*“我看到错误说法3在pthread_create如何解决它! “*。第一步:阅读错误消息。 – user2079303

+0

错误消息“无效对话从void(*)(ssl *)到void *(*)(void *)[-fpermisive]” – user8178737

+0

您应该将相关信息放入问题中。 – user2079303

回答

0

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

你的功能Servlet需要SSL*而不是void*类型的参数,返回void而非void*。因此,您的功能不能转换为其他功能指针类型,因此您对pthread_create的呼叫格式不正确。

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

另一种方法:使用来自C++标准库而不是pthread的std::thread。我自己

+0

tnx您的回复。 – user8178737

0

答:

用于调用pthread_create:

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

,并开始日常的身体:

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