Article View: borland.public.cppbuilder.nativeapi
Article #8459Re: Threads, Functions and Classes
From: Alan Bellingham
Date: Tue, 26 Oct 2004 12:37
Date: Tue, 26 Oct 2004 12:37
50 lines
2451 bytes
2451 bytes
"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:
<2ngsn09863v09thnhue7rtap2h09p2clg5@4ax.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!news.wiretrip.org!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!npeer.de.kpn-eurorings.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<417e2ee6@newsgroups.borland.com> <417e3939@newsgroups.borland.com> <417e407c$1@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: Alan Bellingham <alanb@episys.com> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: Threads, Functions and Classes Date: Tue, 26 Oct 2004 13:37:34 +0100 Reply-To: alanb@episys.com Message-ID: <2ngsn09863v09thnhue7rtap2h09p2clg5@4ax.com> References: <417e2ee6@newsgroups.borland.com> <417e3939@newsgroups.borland.com> <417e407c$1@newsgroups.borland.com> X-Newsreader: Forte Agent 2.0/32.652 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 193.129.251.130 X-Trace: newsgroups.borland.com 1098794253 193.129.251.130 (26 Oct 2004 05:37:33 -0700) Lines: 50 Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!news.wiretrip.org!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!npeer.de.kpn-eurorings.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail