Virtual function call

E

Erazem Polutnik

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
 
D

diligent.snail

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.
 
J

James Kanze

i have a strange problem, when calling virtual function, using
SDL Thread library. Here is situation:

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top