Sandeep
Sandeep

Reputation: 3277

Attaching Member function of a class in pthread

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.

Upvotes: 2

Views: 1210

Answers (1)

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76541

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

Upvotes: 4

Related Questions