1

I have the following class:

class A
{
    private:
        int starter()
        {
             //TO_DO: pthread_create()
        }

        void* threadStartRoutine( void *pThis );
}

I want to create a thread from inside starter() to run threadStartRoutine(). I get compile time errors with regard to the third argument, which should take the address of the start routine.

What would be the correct way to call pthread_create() to create a new thread that starts executing threadStartRoutine() ?

I have come across articles online that say that most compilers do not allow non-static member functions to be invoked using pthread_create(). Is this true? What is the reason behind this?

I am compiling my program on Linux-x64 using G++.

5

4 Answers 4

2

Declare the threadStartRountine() as static:

static void* threadStartRoutine( void *pThis );

Otherwise, the type of threadStartRoutine() is:

void* (A::*)(void*)

which is not the type of function pointer that pthread_create() requires.

13
  • I cannot. The class is multi-threaded. Making threadStartRoutine static results in memory leak errors that I'm trying to get rid of. Aug 28, 2012 at 13:24
  • @ShaileshTainwala, I am unsure how either of those two arguments prevent you from making the function static?
    – hmjd
    Aug 28, 2012 at 13:26
  • 2
    The sentence "The class is multi-threaded" makes absolutely no sense. That's not how C++ and programming work.
    – Kerrek SB
    Aug 28, 2012 at 13:26
  • 2
    @ShaileshTainwala: Do not make the function static. Instead write a new static function that calls yours, and use this new function as argument to pthread_create(). And everybody happy! Naturally you will need to pass on the this argument so that the static function can call a non-static one. But that's why you have the pThis argument.
    – rodrigo
    Aug 28, 2012 at 13:33
  • 2
    @ShaileshTainwala - they're functions, ie. code, with stack parameters, ie. different for each thread. There is no problem calling a static function from multiple threads, passing a 'this' pointer and then calling a method on 'this'. Problems arise when involving static data mambers that are shared by all callers. Aug 28, 2012 at 13:34
2

Do you have a reason for using pthreads? c++11 is here, why not just use that:

#include <iostream>
#include <thread>

void doWork()
{
   while(true) 
   {
      // Do some work;
      sleep(1); // Rest
      std::cout << "hi from worker." << std::endl;
   }
}

int main(int, char**)
{

  std::thread worker(&doWork);
  std::cout << "hello from main thread, the worker thread is busy." << std::endl;
  worker.join();

  return 0;
}
2

Just use a normal function as a wrapper. As hjmd says, a static function is probably the nicest kind of normal function.

0

If you insist on using the native pthreads interface, then you must provide an ordinary function as the entry point. A typical example:

class A
{
private:
    int starter()
    {
        pthread_t thr;
        int res = pthread_create(&thr, NULL, a_starter, this);
        // ...
    }
public:
    void run();
};

extern "C" void * a_starter(void * p)
{
    A * a = reinterpret_cast<A*>(p);
    a->run();
    return NULL;
}

Your Answer

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

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