Article View: borland.public.cppbuilder.nativeapi
Article #8460Re: Threads, Functions and Classes
From: Alistair
Date: Tue, 26 Oct 2004 12:50
Date: Tue, 26 Oct 2004 12:50
58 lines
2706 bytes
2706 bytes
Ahh, I see! This makes perfect sense now!
Thanks to both of you!
Alistair
"Alan Bellingham" <alanb@episys.com> wrote in message
news:2ngsn09863v09thnhue7rtap2h09p2clg5@4ax.com...
> "Alistair" <Alistair@NoSpam.Sodoff> wrote:
>
>>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?
>
> No, that's not what he said.
>
> What he was telling you is that a normal member function cannot be
> passed to CreateThread, simply because it requires a free (aka 'global')
> function. However, when it calls the function you supplied, it passes a
> void* pointer, the same one you gave it when you called
> ::CreateThread().
>
> You therefore want to use this pointer to carry around an object
> pointer, as this:
>
> class MyThread
> {
> public:
> void CreateThread();
> DWORD ThreadProc2 ();
> static DWORD WINAPI ThreadProc (void* Param);
> );
>
> void
> MyThread::CreateThread()
> {
> // ...
> ::CreateThread(..., ..., ThreadProc, this, ..., ...);
> }
>
> DWORD
> MyThread::ThreadProc(void* Param)
> {
> // static casting to member function
> return static_cast<MyThread*>(Param)->ThreadProc2();
> }
>
> DWORD
> MyThread::ThreadProc2()
> {
> // A normal member function you can do with as you like
> }
>
> If you want multiple threads within a single object, it obviously gets a
> little trickier, but using subobjects will sort that.
>
> Alan Bellingham
> --
> ACCU Conference 2005 - 20-23 April, Randolph Hotel, Oxford, UK
Technical Details
Message-ID:
<417e47fb@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!sn-xit-04!sn-xit-05!sn-xit-14!supernews.com!news-out.visi.com!transit-1.news.visi.com!newsfeed.media.kyoto-u.ac.jp!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<417e2ee6@newsgroups.borland.com> <417e3939@newsgroups.borland.com> <417e407c$1@newsgroups.borland.com> <2ngsn09863v09thnhue7rtap2h09p2clg5@4ax.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> <417e407c$1@newsgroups.borland.com> <2ngsn09863v09thnhue7rtap2h09p2clg5@4ax.com> Subject: Re: Threads, Functions and Classes Date: Tue, 26 Oct 2004 13:50:01 +0100 Lines: 60 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: <417e47fb@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1098795003 81.152.249.12 (26 Oct 2004 05:50:03 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!sn-xit-04!sn-xit-05!sn-xit-14!supernews.com!news-out.visi.com!transit-1.news.visi.com!newsfeed.media.kyoto-u.ac.jp!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail