0

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.

jww
  • 97,681
  • 90
  • 411
  • 885
Avb Avb
  • 545
  • 2
  • 13
  • 25
  • 1
    In static ThreadProc function you will check the instance passed as void * and you will execute a separate function. – HAL9000 Feb 06 '14 at 09:28
  • Take the parenthesis out, eg '&ClassB::ThreadProc' – Sean Feb 06 '14 at 09:31
  • possible duplicate of [pthread Function from a Class](http://stackoverflow.com/questions/1151582/pthread-function-from-a-class) – nos Feb 06 '14 at 09:32
  • @HAL9000 That is right. In ThreadProc function, I am casting the object like ClassB *pclassB = reinterpret_cast(p); and calling the object function like pclassB->Process(); so is not it problematic to define ThreadProc as static in case I create multiple threads? I thought this could cause a problem. – Avb Avb Feb 06 '14 at 09:32
  • @nos but your reference does not explain why static definition is needed. – Avb Avb Feb 06 '14 at 09:36
  • @AvbAvb: No, there's no problem calling a static function from multiple threads. Perhaps you're thinking of static *data*; you might get problems if you try to access that from multiple threads. But there's none of that here. – Mike Seymour Feb 06 '14 at 09:36
  • @Mike Seymour But then what is the logic behind defining it static? I did not understand it. – Avb Avb Feb 06 '14 at 09:38
  • 3
    @AvbAvb: The static function is needed because `pthread_create` needs a normal function pointer, not a pointer-to-member, since POSIX is a C API and knows nothing about C++ classes. Such a pointer can only point to a static member or non-member function. These days, C++ has a standard thread library, which is rather more convenient to use, e.g. `thread t([this]{ThreadProc();});` – Mike Seymour Feb 06 '14 at 09:39
  • @AvbAvb Yes, it does explain that. It explains that a non-static member method has a `this` pointer, meaning the C++ member function pointer you pass to pthread_create is not compatible with the C function `pthread_create` - which is not the case for a static member function. (Ofcourse, you could use a non-static, non-member function too) – nos Feb 06 '14 at 09:47
  • @Mike Seymour: In the ThreadProc, I call a funcA of an object. So if I have two threads running, then two objects are calling their funcAs. And objects are calling funcA indefinitely in ThreadProc with while loop. Since ThreadProc is static, so there is single instance which calls funcAs. How is the scheduling of these calls are done in the background? Is it random or always one following the other? – Avb Avb Feb 18 '14 at 15:38

0 Answers0