pthread with without static member function

I have a ClassB which have a thread object and a function defined as:

pthread_t m_thWorkThread;
void* ThreadProc(void *);

And in the constructor of ClassB, I create a thread like:

pthread_create(&m_thWorkThread, NULL, &(ClassB::ThreadProc), this);

However I got two error messages:

error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function.

cannot convert âvoid* (ClassB::)(void)â to âvoid* ()(void)â for argument â3â to âint pthread_create(pthread_t*, const pthread_attr_t*, void* ()(void), void*)â pthread_create(&m_thWorkThread, NULL, &(ClassB::ThreadProc), this);

However if I define the function as static, it does not complain:

static void* ThreadProc(void *);

However I do not want it to be static, since I will create multiple threads and I want each thread has a separate ThreadProc function.

链接地址: http://www.djcxy.com/p/96374.html

上一篇: 通过静态指针访问成员变量实例

下一篇: 没有静态成员函数的pthread