Solved

Pass member function to pthread_create

Posted on 2007-04-05
2
1,213 Views
Last Modified: 2008-10-20
Apparently it's possible to pass a class member function to pthread_create if the member function is declared static.  At least, the following code compiles on gcc with no warnings:

class test {
      public:
      static void* threadf(void* args)
      {
            cout << "testing..." << endl;
            return NULL;
      }
};

int main() {
      test t;

      pthread_t thread;
      pthread_create(&thread, NULL, t.threadf, NULL);      
      pthread_join(thread, NULL);
}

Question:

Is this portable?  Is this allowed by the standard, or is gcc simply allowing this.
0
Comment
Question by:chsalvia
2 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
ID: 18862466
Yes, this is allowed by standard. Static class method is actually global function, which can be accessed through class name. Non-static method has hidden "this" parameter. Both static method and global function don't have hidden parameter, this is the difference.
0
 
LVL 11

Assisted Solution

by:DeepuAbrahamK
DeepuAbrahamK earned 125 total points
ID: 18862784
Yes,static class methods are global hence it can be accessed.

class MyThread
{
....
static void* __stdcall ThreadProc(LPVOID lpVoid);
...
};

call this ThreadProc using _beginthread/ex/CreateThread..

HANDLE h_Thread;
h_Thread = (HANDLE)_beginthreadex(NULL,// Security
                  0,// Stack size - use default
                  ThreadProc,// Thread fn entry point -> note we have not used the class object here
                  (void*)this,// Param for thread any class object etc..if it is there else NULL
                  0,// Init flag
                  &nThreadID);// Thread address
Best Regards,
DeepuAbrahamK
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question