Accelerating thread destroys polymorphism

duplicate: "pure virtual method called" when implementing boost :: thread wrapper interface

I am trying to create a more object oriented version of streams using boost streams.

So, I created the Thread class:

class Thread {
public:
    Thread() {}
    virtual ~Thread() { thisThread->join(); }

    void start() { thisThread = new boost::thread(&Thread::run, this); }

    virtual void run() {};

private:
    boost::thread *thisThread;
};

this class creates a stream in start () for example:

thisThread = new boost::thread(&Thread::run, this);

The problem is that when I create a class that overwrites a method run(), the method run()from Thread is a thread call instead of a new methodrun()

for example, I have a class that extends Thread:

class CmdWorker: public Thread {
public:
    CmdWorker() : Thread() {}
    virtual ~CmdWorker() {}

    void run() { /* deosn't get called by the thread */ }
};

when i do

Thread *thread = new CmdWorker();
thread.start(); //---> calls run() from Thread instead of run() from CmdWorker

but for clarity:

thread.run();  calls the correct run from CmdWorker, (run() is virtual from Runnable)

Any idea why this is happening and how it can be fixed?

Note: I created a function (which has nothing to do with the Thread class)

void callRun(Thread* thread) {
    thread->run();
}

and changed the thread creation to:

thisThread = new boost::thread(callRun, this);

, thread Thread CmdWorker

EDIT:

: http://ideone.com/fqMLF http://ideone.com/Tmva1

( , )

boost

+5
4

:

" , " boost:: thread wrapper

, boost:: thread , , , , .

join, , .

+4

, Thread CmdWorker

, CmdWorker (.. ) Thread - ?

?

+3

delete , . run :

  • , vtable
  • Thread:: run ( thunk)
  • , run()

sleep(1) start, delete , , .

+2

By executing &Thread::Runfor a non-virtual function, you force any class that derives from Thread to use the function specified in the base class of Thread. Try to do Thread :: start a virtual void and see if it fixes your problem.

+1
source

All Articles