Re: Virtual function call
On Jan 21, 10:43 pm, "Erazem Polutnik" <erazem_polut...@hotmail.com>
wrote:
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;
}
There's a fundamental conceptual problem here already. At least
I think there's one---I'm not sure what SDL_CreateThread
actually does. But if it starts the thread (i.e. if it has
semantics something like pthread_create), then invoking it in
the constructor of a base class is a serious error---which may
or may not be immediately apparent in tests, depending on any
number of random variables in the surrounding system.
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.
Those are the aleas of the scheduler. In general, thread
classes should NOT be polymorphic, unless they have a separate
function for starting the thread. Otherwise, you risk starting
the thread in a class before its constructor has finished. For
customization of a thread class, use the strategy pattern, not
the template method pattern.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34