考虑一下多线程代码,在设计上,App为了获取更多的功能,从Window派生,而App同时为了获取
某个模块的回调(所谓的Listener),App同时派生Listener,并将自己的指针交给另一个模块,
另一个模块通过该指针多态回调到App的实现(对Listener规定的接口的implemention)。设计上
只是一个很简单的Listener回调,在单线程模式下一切都很正常(后面我会罗列代码),但是换到
多线程下,编译器似乎就对语言机制的支持不够了:
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**////
/// to demonstrate the fucking bug.
///
#include <iostream>
#include <process.h>
#include <windows.h>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
class Window
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
virtual void wrong()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
std::cout << "wrong" << std::endl;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
virtual ~Window()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
std::cout << "~Window" << std::endl;
}
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
class Listener
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// as most listener class, it only put some interface here
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
virtual void show()
{}
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
class Game
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Game() : _listener( 0 )
{ }
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
void init( Listener *listener )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_listener = listener;
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// it will call Window::wrong function but not App::show.
_listener->show();
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
private:
Listener *_listener;
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
Game gGame;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
static unsigned int __stdcall ThreadFunc( void *p )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
Listener *listener = (Listener*) p;
gGame.init( listener );
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while( true )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
std::cout << ".";
Sleep( 100 );
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
_endthreadex( 0 );
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
class App : public Window, public Listener
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
void init()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// create the game thread
_game_thread = (HANDLE)_beginthreadex( NULL, 0, ThreadFunc, this, 0, NULL );
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// implement the interface
void show()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
std::cout << "App::show" << std::endl;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// exit
void exit()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// just for testing purpose
::TerminateThread( _game_thread, 1 );
::CloseHandle( _game_thread );
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
private:
HANDLE _game_thread;
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
App gApp;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
gApp.init();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
std::cout << "Press enter key to exit!" << std::endl;
std::cin.get();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
gApp.exit();
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
App多重继承Window和Listener,在Game里回调App::show时,却调用到了Window::wrong函数。看上去,传给
Game的Listener指针所指向的虚函数表错误了(vtable指针错了)。App先继承Listener后继承Window时,情况
就正确了。(因为使用了_beginthreadex,程序需要链接多线程的运行时库)
单线程情况下:
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///
/// to demonstrate the fucking bug.
///
/// OK, even it links the multi-thread crt.
#include <iostream>
#include "kl_thread.h"
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
class Window
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**////
virtual ~Window()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
std::cout << "~Window" << std::endl;
}
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
class Listener
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// as most listener class, it only put some interface here
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
virtual void show()
{}
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**////
Listener *gListener;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
class App : public Window, public Listener
//class App : public Listener, public Base
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
void init()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
gListener = this;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// implement the interface
void show()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
std::cout << "App::show" << std::endl;
}
};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
App gApp;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif)
{
gApp.init();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
gListener->show();
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
无论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.cppblog.com/kevinlynx/archive/2008/04/24/48001.html
posted on 2012-09-14 16:16
王海光 阅读(696)
评论(0) 编辑 收藏 引用 所属分类:
C++