Article View: borland.public.cppbuilder.nativeapi
Article #4696Re: Passing Class Functions To CreateThread, etc...
From: Ed Mulroy
Date: Sun, 04 Dec 2005 20:19
Date: Sun, 04 Dec 2005 20:19
62 lines
3034 bytes
3034 bytes
I suggest that _beginthread or _beginthreadNT be used instead of
CreateThread. While CreateThread works correctly, the use of one of the
other two functions is required for proper operation of the runtime library.
You MIGHT get away with CreateThread but it is more likely that the program
would display serious problems that are difficult to track down because they
only appear at irregular intervals.
_beginthread and _beginthreadNT are documented in the compiler's help.
. Ed
> Thomas Maeder wrote in message
> news:m2zmngr8wf.fsf@madbox2.Arbeitsgruppe...
>
> 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:
<43934f2a$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!novia!nx02.iad01.newshosting.com!newshosting.com!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<4392dfb4$1@newsgroups.borland.com> <m2zmngr8wf.fsf@madbox2.Arbeitsgruppe>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,9984129712b6d0f1 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Ed Mulroy" <dont_email_me@bitbuc.ket> Newsgroups: borland.public.cppbuilder.nativeapi References: <4392dfb4$1@newsgroups.borland.com> <m2zmngr8wf.fsf@madbox2.Arbeitsgruppe> Subject: Re: Passing Class Functions To CreateThread, etc... Date: Sun, 4 Dec 2005 15:19:35 -0500 Lines: 64 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2670 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 69.134.170.196 Message-ID: <43934f2a$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1133727530 69.134.170.196 (4 Dec 2005 12:18:50 -0700) X-Authenticated-User: edmulroy Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!novia!nx02.iad01.newshosting.com!newshosting.com!news-out.newsfeeds.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail