Solved

Threading a Member function

Posted on 1997-07-15
2
838 Views
Last Modified: 2008-02-26
I am trying to create a thread from inside a class using another class member.  Here is an example of what I want to do.  The function I want to thread is of type "unsigned long _stdcall A::one(LPVOID)" which would work if I didn't have the "A::" scope operator.  Can I type cast this and remove the scope operator?
Or since the CreateThread function is not a member of the class A, it would violate the Data Abstraction that exists inside classes.  But that should be a runtime error not a compile error, right?

class A{
  one();
  unsigned long _stdcall two(LPVOID);
  int hThread;
  HANDLE ThreadID;
  };
unsigned long _stdcall A::two(LPVOID D){
  //Do Some Stuff
  }
void A::one(){
  char buff[100];
  void *data=&buff;
  hThread=CreateThread (NULL, 0, two, data, 0, &ThreadID);
  }
0
Comment
Question by:lordx
2 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 150 total points
ID: 1165151
Make the function as a static function of class A or a global function.
class A{
...
static unsigned long _stdcall two(LPVOID);
...
};

You have to use a static function or a global function for a thread. The reason is that a member function contains an implicit parameter (the this pointer).
For example,

unsigned long _stdcall two(LPVOID);  // member function of class A

is actually

unsigned long _stdcall two(class A *pThis, LPVOID);

if it is not a static function. And this prototype does not conform to what CreateThread() requires.  

You can pass some data that the thread needs into the structure pointed by LPVOID pParam.
0
 
LVL 4

Expert Comment

by:md041797
ID: 1165152
What I usually do in this type of situation is to use a global function that's a frind of the class.  Then I pass the object in as the data and then cast it to a "SurrogateThis".
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

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 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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

772 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