考虑一下多线程代码,在设计上,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;
}
/**/
///
///
/// 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;
}
<script type="text/javascript"> // </script>
某个模块的回调(所谓的Listener),App同时派生Listener,并将自己的指针交给另一个模块,
另一个模块通过该指针多态回调到App的实现(对Listener规定的接口的implemention)。设计上
只是一个很简单的Listener回调,在单线程模式下一切都很正常(后面我会罗列代码),但是换到
多线程下,编译器似乎就对语言机制的支持不够了:














































































































































App多重继承Window和Listener,在Game里回调App::show时,却调用到了Window::wrong函数。看上去,传给
Game的Listener指针所指向的虚函数表错误了(vtable指针错了)。App先继承Listener后继承Window时,情况
就正确了。(因为使用了_beginthreadex,程序需要链接多线程的运行时库)
单线程情况下:














































































无论Listener, Window的顺序如何,一切都很正常。这起码说明了,在语言层次,我的做法是正确的。
而这个时候即使链接了多线程的运行时库,结果也是正确的。
那么错误可以归结于多线程,可能是在多线程下编译器对虚函数表初始化不正确所致。这是否真的是
VC2003、VC2005的BUG?
posted on 2008-04-24 14:40 Kevin Lynx 阅读(866) 评论(10) 编辑 收藏 引用 所属分类: C++

评论
还是写出来看的清楚:static unsigned int __stdcall ThreadFunc( void *p )
{
Listener *listener = (Listener*) p;
应该是
Listener *listener = (App*) p;
回复 更多评论
# re: 多线程下vc2003,vc2005对虚函数表处理的BUG? 2008-04-24 17:34 giscn
或者这样
_game_thread = (HANDLE)_beginthreadex( NULL, 0, ThreadFunc, (Listener*)this, 0, NULL ); 回复 更多评论
# re: 多线程下vc2003,vc2005对虚函数表处理的BUG? 2008-04-24 17:40 giscn
错误由void* 指针转换引起,与多线程无关,C++ 的指针转换不同于C, 如果是多集继承,参数同样是 this, 其实际值不一定相同,取决于参数类型 回复 更多评论
# re: 多线程下vc2003,vc2005对虚函数表处理的BUG? 2008-04-24 17:40 eXile
楼上的正解。在使用多重继承时要注意对象的布局。 回复 更多评论
# re: 多线程下vc2003,vc2005对虚函数表处理的BUG? 2008-04-24 18:09 亨德列克
Listener *listener = (Listener*) p; 是这一行错了,这个错误应该很多人都会犯…… 回复 更多评论
# re: 多线程下vc2003,vc2005对虚函数表处理的BUG? 2008-04-24 19:18 Kevin Lynx
@亨德列克
不是那一行错了,App和Game两个类分属不同模块,为了不让两个模块耦合,这里使用Listener *listener = (Listener*) p,而不是(App*)p。
giscn和eXile (他删除了他的第二条评论:) )的方法是正确的。可以被采用,再次表示感谢。
这让我意识到,void*在C++里缺乏安全性。 回复 更多评论
# re: 多线程下vc2003,vc2005对虚函数表处理的BUG? 2008-04-24 22:49 饭中淹
这不是多线程的问题
当你先继承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,也不是多线程的问题。
回复 更多评论
# re: 多线程下vc2003,vc2005对虚函数表处理的BUG? 2008-04-25 08:32 FongLuo
收藏,收藏,^_^ 回复 更多评论
# re: 多线程下vc2003,vc2005对虚函数表处理的BUG? 2008-04-25 17:03 #Ant
多继承下还有这样的问题,学习了。。。 回复 更多评论
# re: 多线程下vc2003,vc2005对虚函数表处理的BUG?[未登录] 2008-04-26 00:49 杨粼波
指针偏移,多线程会发生这样滴问题。。。。