Re: Threads on class,
From: Carl Daniel [VC++ MVP] (cpdaniel_remove_this_and_nospam_at_mvps.org.nospam)
Date: 10/24/04
- Next message: Bálint Kátay: "PRB - serial gateway device implementation"
- Previous message: Carl Daniel [VC++ MVP]: "Re: Threads on class,"
- In reply to: Carl Daniel [VC++ MVP]: "Re: Threads on class,"
- Next in thread: Sigurd Stenersen: "Re: Threads on class,"
- Reply: Sigurd Stenersen: "Re: Threads on class,"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 23 Oct 2004 20:25:46 -0700
Carl Daniel [VC++ MVP] wrote:
> After more experimentation, I have to admit that I was wrong - the
> VC++ compiler actually goes to great pains to make this undefined
> behavior "work" by generating thunks that deal with all of the
> different cases correctly.
I wasn't entirely wrong - it's just a bit more subtle than I thought.
The compiler does go to great pains to make sure that the hack will always
call the right function. It does not, however, guarantee that the correct
function will be called with the correct 'this' pointer.
Here's an example of the kind of construct that fails:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
template<class T>
inline void StartMemberThread(T &t, DWORD (__stdcall T::*pmfnMember)())
{
DWORD ThreadId;
CreateThread(NULL, 0, * (LPTHREAD_START_ROUTINE *) &pmfnMember, &t, 0,
&ThreadId);
}
struct B
{
virtual void f() {};
};
struct C
{
int m_i;
C(int i) : m_i(i) {}
DWORD __stdcall MemberThread()
{
printf("In A::MemberThread %d\n",m_i);
return 0;
}
};
struct A : B, C
{
A(int i) : C(i) {}
};
void main()
{
A a(5);
StartMemberThread<A>(a, A::MemberThread);
Sleep(100);
}
If you reverse the order of inheritance of A to C, B then it will "work".
-cd
- Next message: Bálint Kátay: "PRB - serial gateway device implementation"
- Previous message: Carl Daniel [VC++ MVP]: "Re: Threads on class,"
- In reply to: Carl Daniel [VC++ MVP]: "Re: Threads on class,"
- Next in thread: Sigurd Stenersen: "Re: Threads on class,"
- Reply: Sigurd Stenersen: "Re: Threads on class,"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|