Solved

Pass member function to pthread_create

Posted on 2007-04-05
2
1,212 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now