2
pthread_t thread1;
pthread_create(&thread1,NULL,.......,NULL);
// Here I want to attach a thread to a member function of class

How can I pass the member function of a class in the above code.

1
  • Don't forget to catch all exceptions. If a thread exits because an exception completely unwinds the threads stack your program is likely to terminate. Commented Mar 17, 2010 at 8:59

1 Answer 1

4

You need to create a free extern "C" function as a trampoline:

class foo
{
public:
    void *thread_func();
};

extern "C" void *thread_func(void *arg)
{
    return static_cast<foo *>(arg)->thread_func();
}

foo f;
pthread_create(..., thread_func, &f);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.