S
Sebastian Kloeppel
Hi,
i try to write a c++ Posix Thread Wrapper Class. But my virtual Function
doesn't work. Below u see the code :
[thread.h] --------------------
extern "C" {
#include<pthread.h>
}
#include<unistd.h>
#define sout std::cout
#define dout std::cout
class thread;
void* doThread(void* param);
class thread {
private:
friend void* doThread(void* param);
pthread_t tid;
protected:
void* (*todoThread) (void*);
pthread_attr_t tattribute;
bool running;
public:
thread();
thread(void* (*todo)(void*));
thread(void* (*todo)(void*), void* param);
void start(void* param);
void stop(void* status);
void wait();
virtual void mainThread() = 0;
virtual ~thread();
};
// ########## thread ##########
thread::thread() {
running = false;
todoThread = NULL;
pthread_attr_init(&tattribute);
}
thread::thread(void* (*todo)(void*)) {
running = false;
pthread_attr_init(&tattribute);
todoThread = todo;
}
thread::thread(void* (*todo)(void*), void* param) {
running = false;
pthread_attr_init(&tattribute);
todoThread = todo;
start(param);
}
void thread::start(void* param) {
// i do not use the parameter yet ....
if (pthread_create(&tid, &tattribute , doThread, this) != 0) {
dout << "Couldn't create Thread\n";
}
}
void thread::wait() {
if (running) {
pthread_join(tid,NULL);
} else {
dout << "Thread is not running";
}
}
void* doThread(void* param) {
thread* p = (thread*) param;
p->mainThread();
return NULL;
}
void thread::stop(void* status) {
if (running) {
pthread_exit(status);
} else {
dout << "Thread is nor running\n";
}
}
thread::~thread() {
wait();
}
[main.cc] -----------------
#include<iostream>
#include<thread.h>
class athread : public thread {
public:
athread() : thread(NULL,NULL) {
;
}
void mainThread() {
while(true) {
sleep(1);
std::cout << "Thread works";
}
return;
}
};
int main (int argc, char** arqgv) {
athread t1;
t1.wait();
std::cout << "finish\n";
exit(0);
}
//-------------------------------------------------------
After compiling and executing, i get the following result :
pure virtual method called
Aborted
Maybe someone could help and knows why the virtual function isn't
really virtual ...
thanks in advance,
Sebastian Klöppel
i try to write a c++ Posix Thread Wrapper Class. But my virtual Function
doesn't work. Below u see the code :
[thread.h] --------------------
extern "C" {
#include<pthread.h>
}
#include<unistd.h>
#define sout std::cout
#define dout std::cout
class thread;
void* doThread(void* param);
class thread {
private:
friend void* doThread(void* param);
pthread_t tid;
protected:
void* (*todoThread) (void*);
pthread_attr_t tattribute;
bool running;
public:
thread();
thread(void* (*todo)(void*));
thread(void* (*todo)(void*), void* param);
void start(void* param);
void stop(void* status);
void wait();
virtual void mainThread() = 0;
virtual ~thread();
};
// ########## thread ##########
thread::thread() {
running = false;
todoThread = NULL;
pthread_attr_init(&tattribute);
}
thread::thread(void* (*todo)(void*)) {
running = false;
pthread_attr_init(&tattribute);
todoThread = todo;
}
thread::thread(void* (*todo)(void*), void* param) {
running = false;
pthread_attr_init(&tattribute);
todoThread = todo;
start(param);
}
void thread::start(void* param) {
// i do not use the parameter yet ....
if (pthread_create(&tid, &tattribute , doThread, this) != 0) {
dout << "Couldn't create Thread\n";
}
}
void thread::wait() {
if (running) {
pthread_join(tid,NULL);
} else {
dout << "Thread is not running";
}
}
void* doThread(void* param) {
thread* p = (thread*) param;
p->mainThread();
return NULL;
}
void thread::stop(void* status) {
if (running) {
pthread_exit(status);
} else {
dout << "Thread is nor running\n";
}
}
thread::~thread() {
wait();
}
[main.cc] -----------------
#include<iostream>
#include<thread.h>
class athread : public thread {
public:
athread() : thread(NULL,NULL) {
;
}
void mainThread() {
while(true) {
sleep(1);
std::cout << "Thread works";
}
return;
}
};
int main (int argc, char** arqgv) {
athread t1;
t1.wait();
std::cout << "finish\n";
exit(0);
}
//-------------------------------------------------------
After compiling and executing, i get the following result :
pure virtual method called
Aborted
Maybe someone could help and knows why the virtual function isn't
really virtual ...
thanks in advance,
Sebastian Klöppel