3

I want to stop the warning

server.cpp:823: warning: converting from 'void* (ClientHandler::)()' to 'void ()(void)'

in the call:

pthread_create(th, NULL,
    (void* (*)(void*)) &ClientHandler::handle,
    (void *) clientHandler);

where handle() is a member function of ClientHandler:

void* ClientHandler::handle();

I have difficulties deciphering the function-type message from the compiler.

The question is:

  • Should I change the handle() interface? Can I get rid of casting overall?
  • Should I change the cast? To what exactly?
  • Something completely different?
towi
  • 21,587
  • 28
  • 106
  • 187
  • 2
    You should not pass pointer to a member function instead of function pointer. – BЈовић Jul 26 '11 at 07:26
  • Can you make `ClientHandler::handle` static ? – Sander De Dycker Jul 26 '11 at 07:35
  • @sander: no, I need the `this` pointer to access the instances data members in the callback. Of course, I could pass that one explicitly to a static function. – towi Jul 26 '11 at 09:47
  • @towi: in your original question, you're basically asking about interactions between `C` callbacks and member functions. Your updated questions regard details of the STL - completely unrelated. – Mat Jul 26 '11 at 10:04

3 Answers3

7

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).

Community
  • 1
  • 1
Mat
  • 202,337
  • 40
  • 393
  • 406
  • whooo... I see. actually, it *does* work, but I guess only by accident. Probably non-portable. – towi Jul 26 '11 at 09:40
  • I don't think there is any accident there. Should be standard C++03 with the `static_cast`. – Mat Jul 26 '11 at 12:12
3

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" */
Tugrul Ates
  • 9,451
  • 2
  • 33
  • 59
  • 1
    +1 using static member. You can also use `static_cast – DanS Jul 26 '11 at 07:40
  • @DanS - From an access point of view, the final type of the identifier matters, not the type of the cast. Also, I believe reinterpret cast is more approprate in this example because it reflects the intent of cast ie. recovering a lost type (ClientHandler). A static cast means "try to represent this value in another type, possibly resulting in a different bit pattern". – Tugrul Ates Jul 26 '11 at 16:42
  • @DanS, junjanes: there's an SO question on this very issue: [Should I use static_cast or reinterpret_cast when casting a void* to whatever](http://stackoverflow.com/questions/310451/should-i-use-static-cast-or-reinterpret-cast-when-casting-a-void-to-whatever). – outis Nov 03 '11 at 02:25
  • All C/C++ functions (member or global) are cdecl automatically. You do not need the extern "C" which just marks global functions to have C linkage. – Johannes Overmann Nov 09 '15 at 08:56
1

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.

Sean
  • 3,002
  • 1
  • 26
  • 32