考慮一下多線程代碼,在設計上,App為了獲取更多的功能,從Window派生,而App同時為了獲取
某個模塊的回調(所謂的Listener),App同時派生Listener,并將自己的指針交給另一個模塊,
另一個模塊通過該指針多態回調到App的實現(對Listener規定的接口的implemention)。設計上
只是一個很簡單的Listener回調,在單線程模式下一切都很正常(后面我會羅列代碼),但是換到
多線程下,編譯器似乎就對語言機制的支持不夠了:

/**////
/// to demonstrate the fucking bug.
///
#include <iostream>
#include <process.h>
#include <windows.h>

class Window


{
public:

/**////
virtual void wrong()

{
std::cout << "wrong" << std::endl;
}

virtual ~Window()

{
std::cout << "~Window" << std::endl;
}
};


class Listener


{
public:

/**//// as most listener class, it only put some interface here

virtual void show()
{}
};

class Game


{
public:

Game() : _listener( 0 )
{ }

void init( Listener *listener )

{
_listener = listener;

/**//// it will call Window::wrong function but not App::show.
_listener->show();
}

private:
Listener *_listener;
};

Game gGame;

static unsigned int __stdcall ThreadFunc( void *p )


{
Listener *listener = (Listener*) p;
gGame.init( listener );

while( true )

{
std::cout << ".";
Sleep( 100 );
}

_endthreadex( 0 );
return 0;
}

class App : public Window, public Listener


{
public:
void init()

{
// create the game thread
_game_thread = (HANDLE)_beginthreadex( NULL, 0, ThreadFunc, this, 0, NULL );
}


/**//// implement the interface
void show()

{
std::cout << "App::show" << std::endl;
}


/**//// exit
void exit()

{

/**//// just for testing purpose
::TerminateThread( _game_thread, 1 );
::CloseHandle( _game_thread );
}

private:
HANDLE _game_thread;
};


App gApp;


int main()


{
gApp.init();

std::cout << "Press enter key to exit!" << std::endl;
std::cin.get();

gApp.exit();
return 0;
}

App多重繼承Window和Listener,在Game里回調App::show時,卻調用到了Window::wrong函數。看上去,傳給
Game的Listener指針所指向的虛函數表錯誤了(vtable指針錯了)。App先繼承Listener后繼承Window時,情況
就正確了。(因為使用了_beginthreadex,程序需要鏈接多線程的運行時庫)
單線程情況下:

/**////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///
/// to demonstrate the fucking bug.
///
/// OK, even it links the multi-thread crt.
#include <iostream>
#include "kl_thread.h"

class Window


{
public:

/**////
virtual ~Window()

{
std::cout << "~Window" << std::endl;
}
};


class Listener


{
public:

/**//// as most listener class, it only put some interface here

virtual void show()
{}
};


/**////
Listener *gListener;

class App : public Window, public Listener
//class App : public Listener, public Base


{
public:
void init()

{
gListener = this;
}


/**//// implement the interface
void show()

{
std::cout << "App::show" << std::endl;
}
};


App gApp;


int main()


{
gApp.init();

gListener->show();
return 0;
}

無論Listener, Window的順序如何,一切都很正常。這起碼說明了,在語言層次,我的做法是正確的。
而這個時候即使鏈接了多線程的運行時庫,結果也是正確的。
那么錯誤可以歸結于多線程,可能是在多線程下編譯器對虛函數表初始化不正確所致。這是否真的是
VC2003、VC2005的BUG?