Some of you are using C++ and threads.  Many of you are asking this question:
"Can C++ member functions be used as the startup routine in 
pthread_create?"  In other words, can the function passed in 
pthread_create(...) be a C++ class method.


Answer: NO.  Do not do this.

You may sometimes get away with this.  However, the problem is that
the pthread APIs use functions that have C
linkage and calling conventions.  C++ and C linking conventions are not
required to be identical.   

Some of you have seen examples of where this works.  It works with C++
compilers that use C linking conventions.  g++ is NOT one of them.
Even with those C++ compilers the member function must be static.
If you port this application to another compiler it may not work.

One approach is to have all class objects be declared globally. This implies
that there is a good deal of potential for sharing objects between threads.
When sharing objects between threads, always be aware of which thread is
manipulating the object, which thread is responsible for freeing the object,
and what thread safety issues are created by sharing objects between threads.


The other approach is to wrap a member function with a conventional
C function call.  


An example is provided here.  

  oIn the example code, there is a C++ class that is an abstraction of a pair
   of integers.
  
  oLet's say that you want to pass in one of the member functions of the
   pairint class.  You will define a function (It is called ThreadStartup)
   that will be the "wrapper" C function.  This will be passed in the
   pthread_create(...) call.



#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <strings.h>
#include <pthread.h>
#include <iostream.h>

//This C++ class is an abstract representation of a pair of integer numbers.

class pairint{
public:
   pairint(int, int);
   ~pairint() {};
   void add();
   void printpair();
private:
   int _x;
   int _y;
};

pairint::pairint(int x, int y) {
  _x = x;
  _y = y;
};

void pairint::add() {
  cout << "sum of pair is "<< _x+_y << "\n";
};

void pairint::printpair() {
   cout << "value of x is  " << _x << "\n";
   cout << "value of y is  " << _y << "\n";
};

extern "C" void *ThreadStartup(void *);

int main(int argc, char **argv)
{
  pairint  *t=NULL;
  pthread_t     thread;
  int           rc;
  
  t = new pairint(5,10);

  rc = pthread_create(&thread, NULL, ThreadStartup, (void *)t);
  if (rc) {
    cout << "Failed to create a thread" << "\n";
    exit(EXIT_FAILURE);
  }

  printf("Waiting for thread to complete\n");
  rc = pthread_join(thread, NULL);
  if (rc) {
    cout << "Failed to joint a thread" << "\n";
    exit(EXIT_FAILURE);
  }
  cout << "Tadah" << "\n";
  exit(EXIT_SUCCESS);
}

// This function is a helper function. It has normal C linkage, and is
// the base for newly created pairint objects. It runs the
// add method on the pairint object passed to it (as a void *).
// After this method completes normally (i.e returns),
// we delete the object.

void *ThreadStartup(void *_tgtObject) {
  pairint *tgtObject = (pairint *)_tgtObject;
  tgtObject->add();
  delete tgtObject;
  return (NULL);
}


---------------------

This may not seem very object oriented.  There are C++ thread classes but
the approaches presented here are best if you have already written the code.
