Solved

Create a thread in a class

Posted on 1997-11-04
6
348 Views
Last Modified: 2013-12-03
I am using VC++5.0 in NT.

I want to create a thread in a class when a public function of this class is called. The start address of this thread is  one of the member function of this class. Like this:

class  MyClass
{
    void  Startup();
    void  ThreadRountine();
};



void MyClass::Startup()
{
    ...
    ::CreateThread(..., ThreadRountine, ...);
    ...
};

But I fail to compile this code. Only when the ThreadRountine is a global function, then it is OK.

Is this doable or I miss something here ?

Thanks in advance.

Mike
0
Comment
Question by:mikechen
[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
  • 3
  • 3
6 Comments
 

Accepted Solution

by:
jilang earned 50 total points
ID: 1408177
Yes, it's doable, but the problem is that your ThreadRoutine must either be a global function or a static member function.  I can give you the nitty-gritty C++ details on why if you like, but these are your options.  Note that as a static member function it behaves an awful lot like a global function.  If you need it to access member data of a particular object, you'll need to pass along a pointer to the object as the argument to be passed to ThreadRoutine, e.g. your this pointer.
More details available on request..
0
 

Author Comment

by:mikechen
ID: 1408178
Yes. Please send me some more information in detail. You can e-mail me if you would.  (mikechen@fbcs.fujitsu.com)

Thanks in advance.

Mike

0
 

Expert Comment

by:jilang
ID: 1408179
You may or may not be aware that every normal member
function in C++ has an extra, hidden parameter, a "this"
pointer.  For instance, if you have

class Class {
   int foo;
   int Bar1() {return foo;}
   int Bar2() {return this->foo;}
};

You can refer to foo either by name, as in Bar1, or by
dereferencing the this pointer, as in Bar2.  The this
pointer is a Class * inserted as a hidden, first parameter
to Bar1 and Bar2.

Now for static member functions.  Static member functions
are meant to operate on the class itself, not on any
particular object.  Therefore they do not have any extra
parameters.

The CreateThread routine takes a function pointer to a
function with a normal C prototype, something like
void (*threadFunc)(void *lpVoid).  Normal member functions
can't be used, because CreateThread will not know to
insert a this pointer, nor will the operating system.
Static member functions, on the other hand, do not have
any hidden parameters and will work fine.

So the questions becomes, how do you get the thread routine
to work on the data of a particular object?  The answer is
via the void * parameter to the thread routine.  
Extending the above example,

class Class {
   int foo;
   int Bar1() {return foo;}
   int Bar2() {return this->foo;}
   static void ThreadRoutine(void *);
};

void Class::ThreadRoutine(void *lpVoid)
{
   Class *myThis = (Class *)lpVoid;

   myThis->foo;
}

The thread routine accepts one parameter, a void *.  This
argument is passed to CreateThread; when calling CreateThread,
pass in this as your parameter.  ThreadRoutine then must
cast the void * it receives into a Class *.  It then may
operate on member data, but only through the cast pointer.

Admittedly, this is not the most elegant looking code in
the world.  Another option is to have a ThreadRoutine which
is a normal member function, as in your example, and another
static member function which casts its argument and calls
ThreadRoutine on the object.

Hope this answers your question.  Let me know if you need
additional explanation on any part of this.
--Juan
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:mikechen
ID: 1408180
Thank you very much, Juan.

Two more things:

1. What will be the 3rd parameter in CreateThread() if I declare ThreadRountine() as a static member function?
I got an error:
"error C2664: 'CreateThread' : cannot convert parameter 3 from 'void (void)' to 'unsigned long (__stdcall *)(void *)"

2. Global function and static member function, which one is better ?

Thanks in advance.

Mike

2.
0
 

Expert Comment

by:jilang
ID: 1408181
1.  This is because your static thread function does not have the same prototype as required.  It should be declared,
   long __stcall ThreadRoutine(void *);

2.  I prefer static member function; global function works about the same, a static member function has the added benefit of access protection if desired.

Hope this helps.
0
 

Author Comment

by:mikechen
ID: 1408182
Thank you very much, Juan.

I really appreciate your help.

Mike

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to adjust sheet 1 95
What .NET website keeps me current? 9 80
How to LOAD up a .NET Framework website with data? 2 59
Shared files and folders migration 2 30
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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