Solved

Pass member function to pthread_create

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

706 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

19 Experts available now in Live!

Get 1:1 Help Now