Re: Question on C++ Thread Class and inheritance/polymorphism with
POSIX pthread
On Oct 27, 7:43 pm, Ian Collins <ian-n...@hotmail.com> wrote:
alexroat wrote:
I cannot understand a strange behaviour of the following C++
code. I'm on debian linux with gcc 4.1.2 and I'm using
POSIX pthread. I want to create a class which encapsulates
the logic of threads in order to have a simple Thread
virtual class which must be INHERITABLE, that means, I can
create a thread with custom code simply inheriting Thread
class and overloading void run() method.
Have a look at boost threads, the template approach is more flexible.
It depends. It doesn't work that well with joinable threads,
where you want to recover some values after the join; you need
to introduce an extra level of indirection to work around the
copy. On the other hand, it's perfect for detached threads.
[...]
The strange thing is that's happen only using threads. With the
following code instead everithing works correctly :
MyThread mt;
Thread *p=&mt;
p->run();
OUTPUT :
"hello, I'm the derived thread".
Please,
let me know what is the motivation of this strange behaviour.
main() has returned and mt has been at least partly destroyed.
If you add something like a getchar() after the call to run to
prevent is, you will get a different result.
Or join before returning from main.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34