Is there any automatic way to have AfxBeginThread or some other thread
creation function call a class's member function directly?

I don't see how the "this" pointer would get there.

Re: AfxBeginThread by Victor

Victor
Thu Jul 20 13:31:16 CDT 2006

mike7411@gmail.com wrote:
> Is there any automatic way to have AfxBeginThread or some other thread
> creation function call a class's member function directly?

No.

> I don't see how the "this" pointer would get there.

It won't. There is plenty of information on AfxBeginThread on MSDN and
in the help, have you read it all before asking?

If you're using the first overloaded version of AfxBeginThread, then you
need to pass the address of the _static_ member of your class there. In
it you convert the pParam (second argument) to the pointer to the object
of your class and then call your non-static member function.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask



Re: AfxBeginThread by Igor

Igor
Thu Jul 20 13:38:09 CDT 2006

mike7411@gmail.com wrote:
> Is there any automatic way to have AfxBeginThread or some other thread
> creation function call a class's member function directly?
>
> I don't see how the "this" pointer would get there.

AfxBeginThread and all other thread creation functions I know of take
LPVOID parameter and pass it along to thread proc. You can pass your
'this' pointer there, and have the thread proc turn around and call the
member function. Something like this:

template <class T, DWORD (T::*f)()>
DWORD WINAPI myThreadProc(LPVOID p) {
T* pThis = static_cast<T*>(p);
return (pThis->*f)();
}

class Thread {
public:
DWORD ThreadProc();
};

Thread t;
CreateThread(..., myThreadProc<Thread, &Thread::ThreadProc>, &t, ...);

--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: AfxBeginThread by Bruno

Bruno
Thu Jul 20 13:52:31 CDT 2006

> Is there any automatic way to have AfxBeginThread or some other thread
> creation function call a class's member function directly?
>
> I don't see how the "this" pointer would get there.

Typically, the thread function is a static function, which takes an instance
pointer as its input argument.

When starting the thread, you supply AfxBeginThread with the static function
as thread function, and the pointer to your instance as the thread function
argument.

Calling member functions of a specific instance directly is not possible.

--

Kind regards,
Bruno van Dooren
bruno_nos_pam_van_dooren@hotmail.com
Remove only "_nos_pam"



Re: AfxBeginThread by Ben

Ben
Thu Jul 20 17:49:25 CDT 2006


"Bruno van Dooren [MVP VC++]" <bruno_nos_pam_van_dooren@hotmail.com> wrote
in message news:uMywn2CrGHA.4988@TK2MSFTNGP04.phx.gbl...
>> Is there any automatic way to have AfxBeginThread or some other thread
>> creation function call a class's member function directly?
>>
>> I don't see how the "this" pointer would get there.
>
> Typically, the thread function is a static function, which takes an
> instance pointer as its input argument.
>
> When starting the thread, you supply AfxBeginThread with the static
> function as thread function, and the pointer to your instance as the
> thread function argument.
>
> Calling member functions of a specific instance directly is not possible.

This is because thread procedures must be declared stdcall (typically shown
in the documentation as the WINAPI CALLBACK macros), and instance member
functions can't be stdcall. Even though the compiler treats the this
pointer as a hidden first parameter, the different calling conventions
guarantee incompatibility. For example, on x86, the this pointer is passed
in the ECX register, while thread procedures receive an argument on the
stack.

>
> --
>
> Kind regards,
> Bruno van Dooren
> bruno_nos_pam_van_dooren@hotmail.com
> Remove only "_nos_pam"
>



Re: AfxBeginThread by William

William
Thu Jul 20 18:41:26 CDT 2006

"Ben Voigt" <rbv@nospam.nospam> wrote in message
news:uY1mB7ErGHA.2464@TK2MSFTNGP03.phx.gbl...
> This is because thread procedures must be declared stdcall
> (typically shown in the documentation as the WINAPI
> CALLBACK macros),

Yes.

> and instance member functions can't be stdcall.

Nope. They are not by default but there is nothing that prevents you from
declaring them that way.

Regards,
Will