I'm just starting to learn C ++, and I'm trying to create a Thread class that has the basic functionality of the Java Thread class. What I'm trying to do is make a class that you subclass, write a Run method (which is pure virtual in the base class), create a subclass object, call the start method on it, and you have a stream.
The problem is that the way I use C ++ does not send correctly - it, as the Run function, is not virtual, the Run method of the base class is called.
Here is the code for the header
#ifndef _THREAD_H_ #define _THREAD_H_ #include <pthread.h> class Thread { public: Thread(); void Start(); ~Thread(); protected: virtual void Run() = 0; private: static void *RunWrapper(void *); pthread_t thread; }; #endif
Implementation
#include "thread.h" #include <pthread.h> Thread::Thread() { } void Thread::Start() { pthread_create(&thread, NULL, Thread::RunWrapper, (void *) this); } void *Thread::RunWrapper(void *arg) { Thread *t = (Thread *) arg; t->Run(); return arg; } Thread::~Thread() { pthread_join(thread, NULL); }
And a file that is actually trying to do something
#include <iostream> #include "thread.h" class MyThread : public Thread { protected: void Run() { std::cout << "The thread is runned" << std::endl; } }; int main(void) { MyThread thread; thread.Start(); return 0; }
The error I get the last 10 hours:
pure virtual method called terminate called without an active exception
c ++ linux pthreads
John B. Oct 01 '10 at 0:12 2010-10-01 00:12
source share