Solved

Pass member function to pthread_create

Posted on 2007-04-05
2
1,216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

705 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