Virtual function call

Discussion in 'C++' started by Erazem Polutnik, Jan 21, 2008.

  1. 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
    Erazem Polutnik, Jan 21, 2008
    #1
    1. Advertising

  2. Erazem Polutnik

    Guest

    On Jan 21, 10:43 pm, "Erazem Polutnik" <>
    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.
    , Jan 21, 2008
    #2
    1. Advertising

  3. Erazem Polutnik

    James Kanze Guest

    On Jan 21, 10:43 pm, "Erazem Polutnik" <>
    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:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jan 22, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    2
    Views:
    410
    Jack Klein
    Jun 21, 2004
  2. IK
    Replies:
    2
    Views:
    609
    hemraj
    Jul 23, 2004
  3. Ashwin
    Replies:
    2
    Views:
    345
    Pierre Barbier de Reuille
    Aug 1, 2006
  4. Replies:
    11
    Views:
    691
    James Kanze
    Sep 10, 2006
  5. archimed7592
    Replies:
    4
    Views:
    742
    =?UTF-8?B?UGF3ZcWC?=
    May 30, 2007
Loading...

Share This Page