C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Posix Thread Wrapper [HELP!!!]

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German)
View previous topic :: View next topic  
Author Message
Sebastian Kloeppel
Guest





PostPosted: Fri Apr 02, 2004 7:03 pm    Post subject: Posix Thread Wrapper [HELP!!!] Reply with quote




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 Threadn";
}
}

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 runningn";
}
}

thread::~thread() {
wait();
}





[main.cc] -----------------
#include #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 << "finishn";
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

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Thomas Maeder
Guest





PostPosted: Sat Apr 03, 2004 8:21 am    Post subject: Re: Posix Thread Wrapper [HELP!!!] Reply with quote



Sebastian Kloeppel <s.kloeppel (AT) t-online (DOT) de> writes:

Quote:
Maybe someone could help and knows why the virtual function isn't
really virtual ...

Ist sie, keine Sorge.


Während der Konstruktion von t1 ergibt sich aber folgender "Aufrufstapel":

athread t1
-> athread::athread
-> thread::thread(void* (*todo)(void*), void* param)
-> thread::start();
-> pthread_create(&tid, &tattribute , doThread, this)

Ich weiss nicht genau, was pthread_create() macht, aber ich vermute, sie
setzt den Aufrufstapel u.U. so fort:

-> void* doThread(void* param)
-> thread::mainThread()


Der dynamische Typ des in Konstruktion befindlichen t1 ist zu diesem
Zeitpunkt erst thread. thread::mainThread ist aber pur virtuell und hat keine
Definition.

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Rolf Magnus
Guest





PostPosted: Sat Apr 03, 2004 9:39 am    Post subject: Re: Posix Thread Wrapper [HELP!!!] Reply with quote



Sebastian Kloeppel wrote:

Quote:
Hi,

Hi. Hier schreibt man übrigens in deutscher Sprache.

Quote:
void* doThread(void* param);

Diese Funktion sollte am besten auch extern "C" sein.

Quote:
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 ...

Ist sie schon, aber im Konstruktor bedeutet das etwas anderes, als du
vermutlich meinst. Im Konstruktor von thread existiert der athread-Teil
noch nicht, und daher ist das Objekt zu diesem Zeitpunkt noch vom Typ
thread. Deshalb wird nicht die Implementation in athread aufgerufen.

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Daniel Albuschat
Guest





PostPosted: Sat Apr 03, 2004 10:38 am    Post subject: Re: Posix Thread Wrapper [HELP!!!] Reply with quote

Rolf Magnus wrote:
Quote:
void* doThread(void* param);

Diese Funktion sollte am besten auch extern "C" sein.

Warum? Was stoert dich daran, dass sie nicht extern "C" ist?
Also pthread stoert es auf jeden Fall nicht.
Wie auch, es wird schliesslich eine Funktionsadresse und kein
Funktionsname uebergeben.
Ich glaube, so eine aehnliche Diskussion gab' es hier vor
kurzem schonmal.

cu,
Daniel

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Rolf Magnus
Guest





PostPosted: Sat Apr 03, 2004 12:35 pm    Post subject: Re: Posix Thread Wrapper [HELP!!!] Reply with quote

Daniel Albuschat wrote:

Quote:
Rolf Magnus wrote:
void* doThread(void* param);

Diese Funktion sollte am besten auch extern "C" sein.

Warum? Was stoert dich daran, dass sie nicht extern "C" ist?
Also pthread stoert es auf jeden Fall nicht.
Wie auch, es wird schliesslich eine Funktionsadresse und kein
Funktionsname uebergeben.
Ich glaube, so eine aehnliche Diskussion gab' es hier vor
kurzem schonmal.

Die gibt es regelmäßig, und jedes Mal muß ich erklären, daß extern "C"
nicht nur das "name mangling" beeinflußt, sondern auch die
Aufrufkonvention, also z.B. wie die Parameterübergabe genau
funktioniert. Viele Compiler für C und C++ sind heute kombiniert und
verwenden dieselben Aufrufkonventionen für beide Sprachen, deshalb
funktioniert es meistens auch ohne extern "C", aber es ist streng
genommen trotzdem nicht korrekt.

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Markus Schaaf
Guest





PostPosted: Sat Apr 03, 2004 12:43 pm    Post subject: Re: Posix Thread Wrapper [HELP!!!] Reply with quote

"Daniel Albuschat" <daniel (AT) viming (DOT) de> schrieb:

Quote:
Rolf Magnus wrote:
void* doThread(void* param);

Diese Funktion sollte am besten auch extern "C" sein.

Warum? Was stoert dich daran, dass sie nicht extern "C" ist?
Also pthread stoert es auf jeden Fall nicht.
Wie auch, es wird schliesslich eine Funktionsadresse und kein
Funktionsname uebergeben.
Ich glaube, so eine aehnliche Diskussion gab' es hier vor
kurzem schonmal.

Tja und leider hast Du damals gepennt, wie es scheint. Linkage
beschreibt nicht nur Name-Mangeling, sondern auch Aufrufkonvention.
Das ist jedoch nur die Erklärung. Als Beweis reicht, daß die
Funktion »pthread_create« eine Funktion mit C-Linkage als Argument
erwartet (so sie nicht extra für C++ überladen wurde, was man in
den meisten Fällen ausschließen kann). Und die einzige Möglichkeit,
mit dem C++-Compiler eine Funktion mit C-Linkage zu erzeugen, ist
»extern "C"« davorzuschreiben. Das gilt z.B. auch für »signal«.

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Tibor Pausz
Guest





PostPosted: Sat Apr 03, 2004 2:34 pm    Post subject: Re: Posix Thread Wrapper [HELP!!!] Reply with quote

Daniel Albuschat <daniel (AT) viming (DOT) de> wrote:

Quote:
Ich glaube, so eine aehnliche Diskussion gab' es hier vor
kurzem schonmal.

Die Parameter auf dem Stack können unterschiedlich übergeben werden. Bei
den meisten UNIX C++ Compiler dürfte dürfte eine static member function
statt einer extern C function funktionieren, nur eine Garantie dafür
gibt es nicht.

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.