tags:

views:

127

answers:

2

Hi.. A friend sent me his threading class. Now I just want to run a listener but the thread doesnt want to accept the function. I want to execute the function (defined in static class Networks) THREAD listen(void* args). THREAD is a #define THREAD unsigned __stdcall

Networks::init() {
  listenerth = thread();
  listenerth.run(listen, NULL, "listener");
}

In class thread he defined run as void run( THREAD func(void*), void* args, const char* pname);

How can I get that to run listen in another thread either?

[edit] Error message: main.cpp(19): error C3867: 'Networks::listen': function call missing argument list; use '&Networks::listen' to create a pointer to member

But when i move my mouse to the error place(symbol listen), it shows me this in a tooltip(yes, MS VC++):

unsigned int __stdcall Networks::listen(void* data)

Error: argument of type "unsigned int (__stdcall Networks::*)(void *data)" is incompatible with parameter of type "unsigned int (__stdcall )(void)"

+2  A: 

Is listenerth a member function? If it is, then it won't work for run(..).

Member functions are not the same as functions declared outside of classes. They cannot be used with normal function pointers.

Gunslinger47
Member functions can be used, but they should be declared static.
Patrick
Aww, I just forgot static...Thanks :D
aPoC
+2  A: 

As others say, you can't use a non-static member function here, because it expects a normal function pointer. If you need to call a non-static member (because it needs to access state in the class), then you could use the args argument to call it via a static "trampoline" function, something like this:

unsigned listen() {/* do the interesting stuff */}

static THREAD call_listen(void* me) 
{
    return static_cast<Networks*>(me)->listen();
}

void init()
{
    listener = thread();
    listener.run(call_listen, this, "listener");
}
Mike Seymour