0

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

aPoC
  • 6,375
  • 3
  • 13
  • 8
  • 5
    you should also post the exact error message the compiler gives you. – Axarydax Jun 27 '10 at 07:30
  • Does the first code example of [this answer](http://stackoverflow.com/questions/3108631/how-to-pass-a-pointer-to-a-member-function-to-a-c-function/3108745#3108745) help? – sbi Jun 27 '10 at 08:04
  • 1
    possible duplicate of [Using a C++ class member function as a C callback function](http://stackoverflow.com/questions/1000663/using-a-c-class-member-function-as-a-c-callback-function) – Georg Fritzsche Jun 27 '10 at 08:41

2 Answers2

2

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
  • 6,851
  • 2
  • 18
  • 29
2

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
  • 235,407
  • 25
  • 414
  • 617