Article View: borland.public.cppbuilder.nativeapi
Article #8458Re: Threads, Functions and Classes
From: Alistair
Date: Tue, 26 Oct 2004 12:18
Date: Tue, 26 Oct 2004 12:18
90 lines
4166 bytes
4166 bytes
So what you are saying is that I have no way of including a function in my
class that is passed to the WINAPI CreateThread method?
I would like to have threads handled within the class so anything using the
class doesnt have to worry about anything other than creating the class and
using its public functionality, where the thread is handles in a private
manor and not seen or known about outside the class?
TOMyClass * objectPtr = new TOMyClass ();
int answer = objectPtr->GetAnswer(); //That goes away and creates 5 threads
and processes something
delete objectPtr;
This class should handle everything internally, but your examples dont do
this, as far as I can see.
How would I have a function with a class that can be passed to the WINAPI
CreateThread method?? Surely it must be possible to do all this within a
class??
Also, thanks for the quick reply!
Cheers
Alistair
"Andrue Cope [TeamB]" <no.spam@not.a.valid.address> wrote in message
news:417e3939@newsgroups.borland.com...
> Alistair wrote:
>
>> I realise its because I have defined the function within my class,
>> but I really need it to be defined within my class!!
>
> The problem is that non-static methods must have access to a 'this'
> pointer. C++ passes that invisibly to every such method. Without doing
> that the method has no way of knowing which object it is being called
> on. There is only ever one copy of the code for every method. Only
> object data is replicated for every object and the invisible argument
> is used to identify this data.
>
> One solution is to create an instance of the class and reference that
> from within the function. Pseudo code example:
>
> TOMyClass * objectPtr;
> DWORD callBackFunction( void * ObectPtr )
> {
> objectPtr->DoSomething( ObjectPtr );
> }
>
> You will have to remember to set up the object pointer (objectPtr in
> this example) before passing the function pointer. Note that this also
> doesn't lend itself to calling back multiple objects at the same time.
> By that I mean that if you have ten objects at any one time and want
> any one of them to be called back as needed this won't work.
>
> I note however that the caller is passing a 'void *' to you. That looks
> to me like it's intended to be used as a handle. IOW it is there to
> provide state information to identify the callback and the purpose
> behind it.
>
> If that is the case the solution is to make the 'void *' be a pointer
> to an object so that you can do:
>
> DWORD callBackFunction( void * ObjectPtr )
> {
> reinterpret_cast<TOMyClass *>( ObjectPtr )->DoSomething();
> }
>
> If additional information is needed then an intermediate structure
> could be used:
>
> struct TCallBackInfo
> {
> void * SomeInfo;
> TOMyClass * ObjectPtr;
> };
>
> DWORD callBackFunction( void * ObjectPtr )
> {
> TCallBackInfo * cbi=reinterpret_cast<TCallBackInfo *>( ObjectPtr );
>
> cbi->ObjectPtr->DoSomething( cbi->SomeInfo );
> }
> --
> Andrue Cope [TeamB]
> [Bicester, Uk]
> http://info.borland.com/newsgroups/guide.html
Technical Details
Message-ID:
<417e407c$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!border2.nntp.ams.giganews.com!nntp.giganews.com!news-hub.cableinet.net!blueyonder!news-peer-test!btnet!newsfeed.bt.es!195.5.65.34.MISMATCH!caladan.arrakis.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<417e2ee6@newsgroups.borland.com> <417e3939@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,fc124e2275bfd1e9 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Alistair" <Alistair@NoSpam.Sodoff> Newsgroups: borland.public.cppbuilder.nativeapi References: <417e2ee6@newsgroups.borland.com> <417e3939@newsgroups.borland.com> Subject: Re: Threads, Functions and Classes Date: Tue, 26 Oct 2004 13:18:03 +0100 Lines: 92 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2180 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180 NNTP-Posting-Host: 81.152.249.12 Message-ID: <417e407c$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1098793084 81.152.249.12 (26 Oct 2004 05:18:04 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!border2.nntp.ams.giganews.com!nntp.giganews.com!news-hub.cableinet.net!blueyonder!news-peer-test!btnet!newsfeed.bt.es!195.5.65.34.MISMATCH!caladan.arrakis.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail