考慮一下多線程代碼,在設計上,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?
以下是評論:
這不是多線程的問題
當你先繼承window后繼承Listener的時候,App的內存結構如下:
class App
vt of Window
data of Window
vt of Listener
data of Listener
data of App
_beginthreadex的參數是void*,你把this傳遞進去,相當于傳遞CApp* this。其實隱含的就是Window*this,那么里面調用Listener->Show,自然就會去Window的vt里面查找對應索引的函數,就會調用錯函數。
而第二個,因為你顯式的=this,所以,編譯器會進行轉換,從而把正確的Listener地址賦值給那個全局指針,這時,無論繼承順序如何,都是正確的結果。
這其實是因為對象指針轉換不準確導致的,不是vc的bug,也不是多線程的問題。
Kevin Lynx的另一篇文章:多重繼承和void*的糗事 也作出過解釋。
本文轉自:http://www.shnenglu.com/kevinlynx/archive/2008/04/24/48001.html
posted on 2012-09-14 16:16
王海光 閱讀(718)
評論(0) 編輯 收藏 引用 所屬分類:
C++