Cast member function for create_pthread() call

橙三吉。 提交于 2019-11-29 12:09:30
Mat

You can't do that directly, pointers to member functions are not plain pointers to functions and can't be handed over to C callbacks directly.

You'll need one level of indirection:

void callHandle(void *data) {
  ClientHandle *h = static_cast<ClientHandle*>(data);
  h->handle();
}

pthread_create(th, 0, &callHandle, static_cast<void*>(handle));

See the Pointers to members section of the C++FAQ for more information / alternatives.

For the validity of the cast in callHandle, see this question. You are sole responsible for making sure that handle is still alive and well when callHandle is called of course (and for the fact that it actually points to a ClientHandle).

You need to pass a static cdecl function to pthread_create as given in this signature:

void* handler(void* data);

The optional argument can be used to pass your ClientHandler object into the thread.

class ClientHandler()
{
public:
  static void* handle(void* data);
}

extern "C" {

void* ClientHandler::handle(void* data)
{
  ClientHandler* handler = reinterpret_cast<ClientHandler*>(data)
  // fancy stuff with handler object here
}

} /* extern "C" */

If you're looking to invoke the method ClientHandler::handle from a specific instance of the ClientHandler class, it's unfortunately a bit more complicated than your example as pointers to member functions are different than pointers to functions in general. See here for a complete description of what needs to be done to create pthreads in such a manner.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!