简体   繁体   English

pthread_create参数传递错误

[英]pthread_create argument passing error

Trying to write some simple multithreaded server program and got that error recently: 试着写一些简单的多线程服务器程序并最近得到了这个错误:

Server.cpp:64:64: error: argument of type 'void* (Server::)(void*)' does not match 'void* (* Server.cpp:64:64:error:类型'void *(Server ::)(void *)'的参数与'void *(*)不匹配 )(void *) )(无效 *)

Here is some lines from my code: 以下是我的代码中的一些行:

Header file: 头文件:

class Server
{
public:
void establishConnection(const char * );
...

private:
void *listening(void *);
void *acceptingConnection(void *);
pthread_attr_t attrr;
}

cpp file: cpp文件:

void Server:: establishConnection(const char *port_number )
{
...
  pthread_create(&listn, &attrr, Server::listening, (void*)socketfd);//pthread_t listn, socketfd is a socket destricptor(int)     
  pthread_join(listn, NULL);

}
void* Server::listening(void *arg)
{
  int socketfd = (int)arg;
  ...
}

Normally, if I define thread function prototypes in the cpp file instead of header file, it works properly(without Server:: definition of course) Tried few other things like (void*)Server::listening, listening, (void*)listening but still didnt work. 通常,如果我在cpp文件中定义线程函数原型而不是头文件,它可以正常工作(当然没有Server ::定义)尝试了一些其他的东西,比如(void *)Server :: listening,listening,(void *)listening但仍然没有奏效。 Could you enlighten me? 你能开导我吗? How to pass the method parameter to listening method? 如何将方法参数传递给侦听方法?

Secondly, I am learning c++ currently(already know C), is it true to use some C methods, char* arrays instead of strings, header files in the c++ program? 其次,我正在学习c ++(已经知道C),在c ++程序中使用一些C方法,char *数组而不是字符串,头文件是真的吗? Such as string.h, stdlib.h, pthread.h? 比如string.h,stdlib.h,pthread.h?

You can just read the error message: 您只需阅读错误消息:

type ‘void* (Server::)(void*)’ does not match ‘void* (*)(void*)‘

Because Server::listening is a non-static member function of Server , and a pointer non-static member function cannot possibly be converted to a pointer-to-non-member-function. 因为Server::listening是的非静态成员函数Server ,和一个指针非静态成员函数不可能被转换成一个指针到非成员函数。

You have to make your Server::listening function static , or write a stand-alone function outside the Server class. 您必须使您的Server::listening函数static ,或者在Server类之外编写独立函数。

You need to create a wrapper function for pthread_create() , and from there call into your class method. 您需要为pthread_create()创建一个包装函数,然后调用您的类方法。

class Server
{
...   
private:
int sock;
};

extern "C" void * server_listening (void *arg) {
    Server *s = static_cast<Server *>(arg);
    return s->listening();
}

void Server:: establishConnection(const char *port_number )
{
...
  this->sock = socketfd;
  pthread_create(&listn, &attrr, server_listening, this);
  pthread_join(listn, NULL);
}

The extern "C" linkage on the wrapper function is in place since pthread_create() is a C function, and expects a function pointer with C linkage. 由于pthread_create()是一个C函数,因此包装器函数上的extern "C"链接就位,并且需要一个带C链接的函数指针。 This is important if on your system the C ABI and the C++ ABI are not the same. 如果在您的系统上C ABI和C ++ ABI不相同,这一点很重要。 A static method of a class can only have C++ linkage. 类的静态方法只能具有C ++链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM