Solved

Threading a Member function

Posted on 1997-07-15
2
837 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

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
Want to delete all my personal data 13 137
Interoperability issue between VC++ (ocx) & VB.NET applications 7 74
best sources to up-to-date in C++? 8 70
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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.

911 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

22 Experts available now in Live!

Get 1:1 Help Now