Re: Threads on class,

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Alexander Grigoriev (alegr_at_earthlink.net)
Date: 10/24/04


Date: Sat, 23 Oct 2004 17:13:48 -0700

template<typename T, DWORD (T::*ThreadFunc)()>
DWORD ThreadFuncAdaptor(PVOID * arg)
{
return (static_cast<T*>(arg)->*ThreadFunc)();
}

CreateThread(NULL, 0, ThreadFuncAdaptor<MyClass, & MyClass::ThreadFunc>,
pMyClass,,,);

It's also possible to create an (templated) overload for CreateThread.

"Steve Maillet (eMVP)" <nospam1@EntelechyConsulting.com> wrote in message
news:OlMo6xUuEHA.3860@TK2MSFTNGP09.phx.gbl...
> I've never said it didn't or wouldn't work - I just said the technique had
> some flaws as a general solution to using C++ class member functions as
> threads. You've posted updated code to resolve the items I mentioned. (and
> they now appear in my news reader). The claim that it won't work on an x86
> was wrong. He clearly missed the use of __stdcall in there and how it
> resolves that particular issue.
>
> I don't think code samples are really all that necessary as Carl has
> defined rather explicitly where the problem lies and the types of code
> structure (or compiler switches) that force the compiler to use a pointer
> to member function representation that is incompatible with the pointer
> casting technique used in the solution thus far. Getting around that
> requires more significant effort.
>
> I have not posted code for getting around the pointer to member type issue
> as I cannot legally do so. My company has a framework for threading that
> resolves that issue and I can't just post that code, sorry. It is
> currently a small part of a larger solution for BSPs and thus not priced
> in a range useful for someone doing application development. (If all you
> wanted was the threading library it probably would be cheaper to do it
> yourself) I'm now working to see if we can get that pulled out as a more
> reasonably priced stand alone product. We'll see what comes of that.
>
> --
> Steve Maillet
> EmbeddedFusion
> www.EmbeddedFusion.com
> smaillet at EmbeddedFusion dot com
>
>



Relevant Pages

  • Re: Threads on class,
    ... DWORD ThreadFuncAdaptor ... > structure that force the compiler to use a pointer ... > resolves that issue and I can't just post that code, ... > wanted was the threading library it probably would be cheaper to do it ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: "Portability" contructs like UINT32 etc.
    ... All those WORDS, DWORDS, LPCSTR, HPARAMS, LPARAMS etc. ... I have a real problem with "WORD" and "DWORD" which is used in Windows programs a lot: "WORD" is defined as a 16 bit type and DWORD as a 32 bit type, which means that on your average Pentium or Athlon processor a WORD is a halfword and a DWORD is a word, whereas on a 64 bit processor a WORD is a quarterword and a DWORD is a ... And LPCSTR - "Long Pointer to C String". ... You see, char* is typedef'ed to PCCHAR (yes, ...
    (comp.lang.c)
  • Help Converting To C#
    ... DWORD IBT_DeviceDiscovery; ... pbtDev is a pointer to an allocated array of IBTDEVICE structures. ... This function creates an SDP record and registers a Bluetooth service within ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Which assembler can handle the BIG stuff ?
    ... >>> Instructtions for finaly pointing to the Pointer, ... >> What's a checksum table, ... then a rep cmpsd (dword compare) could be pretty fast indeed. ... This kind of Hash Table is ...
    (alt.lang.asm)
  • Re: CListCtrl, custom item data and allocation policy
    ... This is a good example of where to use inheritance. ... CMyListCtrl use DWORDs and then inherit from CMyListCtrl and have it use ... The problem is it takes a DWORD argumentr that can reference a value or a ... pointer to an allocated structure. ...
    (microsoft.public.vc.mfc)