🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #4695

Re: Passing Class Functions To CreateThread, etc...

#4695
From: Thomas Maeder [TeamB]
Date: Sun, 04 Dec 2005 18:53
61 lines
3059 bytes
Alistair <NoSpamPlease@Somewhere.Com> writes:

> Me : "I can't pass the pointer of a class function to CreateThread"
>
> Gambit : "Well, technically you can, but not easily.
> 	You would essentially have to use a proxy function
> 	in order to do it.  Using a static method is easier,
> 	though, especially since CreateThread() allows a
> 	user-defined value to be passed to the thread instance.
> 	That makes a proxy function unnecessary."
>
> You got me thinking Gambit, how would you create a proxy function to
> make a call back of type 'void Do()' take a 'void myclass::Do()'?

That's what I'd try:

CreateThread() is declared like this:

HANDLE CreateThread(
  LPSECURITY_ATTRIBUTES lpThreadAttributes,
  SIZE_T dwStackSize,
  LPTHREAD_START_ROUTINE lpStartAddress,
  LPVOID lpParameter,
  DWORD dwCreationFlags,
  LPDWORD lpThreadId
);

lpStartAddress can be used to pass the address of the callback
function, and lpParameter for the address of the callback object.

So something like this should work:

class ThreadFunction
{
  public:
    virtual DWORD execute() = 0;

    HANDLE startThread();
};

DWORD WINAPI callbackFct(LPVOID lpParameter)
{
  assert(lpParameter!=0);
  ThreadFunction *tf(static_cast<ThreadFunction *>(lpParameter));
  return tf->execute();
}

HANDLE ThreadFunction::startThread()
{
  return CreateThread(a1,a2,&callbackFct,this,a3,a4);
  // for some appropriate a1..a4
}

Since the function called back is to return a DWORD, I think that
should also be the return type of execute().

callbackFct is the proxy function. Technically, it can't be a static
member of a class (although you may get away with one using most
implementations), but it can well be a non-member function defined in
the unnamed namespace of the translation unit defining
ThreadFunction::startThread().
Technical Details
Message-ID: <m2zmngr8wf.fsf@madbox2.Arbeitsgruppe>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news2.volia.net!newsfeed01.sul.t-online.de!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!newsfeed.inode.at!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <4392dfb4$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,9984129712b6d0f1
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Passing Class Functions To CreateThread, etc...
References: <4392dfb4$1@newsgroups.borland.com>
From: maeder@glue.ch (Thomas Maeder [TeamB])
Organization: TeamB
X-Face: ":yn4zt^A_dn>6~{YJmu^B\'4w)e:apPM\VC=Dvf&*tASxX9|BYczkFd$xr.$vNSZ[3Uobr p]c4\+\l){!WQI.$]veP({HLd;CDYH?iN
Date: Sun, 04 Dec 2005 19:53:36 +0100
Message-ID: <m2zmngr8wf.fsf@madbox2.Arbeitsgruppe>
User-Agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)
Cancel-Lock: sha1:UwPLbx7/tDq8W8RY0FuLE1dVJo8=
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
NNTP-Posting-Host: 84.73.158.225
X-Trace: newsgroups.borland.com 1133722363 84.73.158.225 (4 Dec 2005 10:52:43 -0700)
Lines: 61
X-Authenticated-User: tmaeder
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news2.volia.net!newsfeed01.sul.t-online.de!newsfeed00.sul.t-online.de!t-online.de!newsfeed.freenet.de!newsfeed.inode.at!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail