4

Given a typical class:

struct Whatever
{
    void Doit();
};

Whatever w;

what is the best way to get the member function to be called by a C void* based callback such as pthread_create() or a signal handler ?

pthread_t pid;

pthread_create(&pid, 0, ... &w.Doit() ... );

9 Answers 9

6

Most C callbacks allow to specify an argument e.g.

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                   void *(*start_routine)(void*), void *arg);

So you could have

void myclass_doit(void* x)
{
  MyClass* c = reinterpret_cast<MyClass*>(x);
  c->doit();
}

pthread_create(..., &myclass_doit, (void*)(&obj));
3
  • just a note that myclass_doit must have c linkage (ie. extern "C"). Sep 26, 2008 at 15:37
  • No, it doesn't. It's clearly being referenced from a C++ file.
    – keraba
    Sep 26, 2008 at 18:08
  • This requires a separate wrapper for each callback. True it works, but it creates a huge mess of code management overhead.
    – Catskul
    Sep 15, 2009 at 16:42
6

The most concise solution is to define, in a header file shared by all your code:

template <typename T, void (T::*M)()>
void* thunk(
    void* p)
{
    T* pt = static_cast<T*>(p);

    (pt->*M)();

    return 0;
}

You probably want to define 4 versions: one each where the thunk returns void and void*, and one each where the member function returns void and void*. That way the compiler can match the best one, depending on the circumstances (and in fact it will complain if everything doesn't match.)

Then all you have to type every time you run into one of these situations is:

pthread_create(&pid, 0, &thunk<Whatever, &Whatever::doit>, &w);

This will even work when the method is private, as long as the method is referenced from within the class's code. (If not, I have to wonder why the code is referencing a private method.)

3

Use a C-function wrapper like this:

struct Whatever
{
    void Doit();
};

extern "C" static int DoItcallback (void * arg)
{
  Whatever * w = (Whatever *) arg;
  w->DoIt();
  return something;
}

Only works if you can pass the pointer to the class somehow. Most callback mechanisms allow this.

Afaik this is the only method to do this. You can't directly call a method from C without lots of hacking.

1
  • This requires a separate wrapper for each callback. True it works, but it creates a huge mess of code management overhead.
    – Catskul
    Sep 15, 2009 at 16:41
3

Is the member function private? If not, use the standard idiom:

void* pthread_foo_caller(void* arg) {
    Foo* foo = static_cast<Foo*>(arg);
    foo->bar();
    return NULL;
}

If the member function is private, you can declare a static method in the class that takes a "this" pointer and calls the appropriate method. For example:

class Foo {
 public:
  static pthread_foo_caller(void* arg);
  ...
};

void* Foo::pthread_foo_caller(void* arg) {
    Foo* foo = static_cast<Foo*>(arg);
    foo->private_bar();
    return NULL;
}
1

The member function MUST be static. Non-static have an implied "this" argument. Pass the pointer to your Whatever instance as the void* so that the static member can get at the instance.

1

Here's a simple way to do it, don't forget to manage the lifetime of your "MemberFunction" object properly.

#include 

class MyClass
{
public:
    void DoStuff()
    {
        printf("Doing Stuff!");
    }
};

struct MemberFunction
{
    virtual ~MemberFunction(){}
    virtual void Invoke() = 0;
};

void InvokeMember(void *ptr)
{
    static_cast(ptr)->Invoke();
}

template 
struct MemberFunctionOnT : MemberFunction
{
    typedef void (T::*function_t)();
public:
    MemberFunctionOnT(T* obj, function_t fun)
    {
        m_obj = obj;
        m_fun = fun;
    }

    void Invoke()
    {
        (m_obj->*m_fun)();
    }
private:
    T *m_obj;
    function_t m_fun;
};

template 

MemberFunction* NewMemberFunction(T *obj, void (T::*fun)())
{ 
    return new MemberFunctionOnT(obj, fun); 
}

//simulate a C-style function offering callback functionality.
void i_will_call_you_later(void (*fun)(void*), void *arg)
{
    fun(arg);
}

int main()
{
    //Sample usage.
    MyClass foo;

    MemberFunction *arg = NewMemberFunction(&foo, &MyClass::DoStuff);
    i_will_call_you_later(&InvokeMember, arg);
    return 0;
}
1

One thing you should be aware of is that if you write code like this:

try {
    CallIntoCFunctionThatCallsMeBack((void *)this, fCallTheDoItFunction);
} catch (MyException &err)
{
   stderr << "badness.";
}

void fCallTheDoItFunction(void *cookie)
{
    MyClass* c = reinterpret_cast<MyClass*>(cookie);
    if (c->IsInvalid())
        throw MyException;
    c->DoIt();
}

You may run into some serious trouble depending on your compiler. It turns out that in some compilers while optimizing, they see a single C call in a try/catch block and exclaim with joy, "I am calling a C function that, because it is good old fashioned C, cannot throw! Calloo-cally! I shall remove all vestiges of the try/catch since it will never be reached.

Silly compiler.

Don't call into C that calls you back and expect to be able to catch.

1
  • Well, if your code is called by C (or other language) code, you need to make sure that no exceptions "escape" back into C/other anyway, so this advice is tautological. :-P Sep 26, 2008 at 23:36
0

See this link

Basically, it's not directly possible, because: "Pointers to non-static members are different to ordinary C function pointers since they need the this-pointer of a class object to be passed. Thus ordinary function pointers and [pointers to] non-static member functions have different and incompatible signatures"

0

While I haven't used it from C, for doing callbacks, I highly recommend looking at libsigc++. It's been exactly what I've needed a number of times when doing C++ callbacks.

1
  • Doesn't libsigc++ define function objects (which are not callable from C) ? Just checking. Otherwise, yes, I agree that it is useful.
    – keraba
    Sep 26, 2008 at 19:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.