Re: Desperation with Boost Threads
Noah Roberts schrieb:
OK. What I'm trying to to: Create a thread (in C-terms speaking I'd like
to do a pthread_create). How do I do it in that constellation then?
The equivalent is the constructor for boost::thread. You can only call
that constructor once. It constructs and spawns a new thread. You will
have a handle to that thread through the boost::thread instance so long
as that instance survives. You can't, however, copy that instance.
Ah, okay, that makes perfect sense then. It seems I totally missed that
when reading the boost docs :-\
So I've changed it to employ your changes. So that I don't have to
fiddle with pointers I changed the behaviour of my Thread class to
behave just like boost::thread does. I threw out Work() and replaced it
by the operator(). Now the relevant parts look like this:
class Thread {
private:
boost::thread ThreadID;
public:
Thread();
Thread(const Thread &Other);
void operator=(const Thread &Other);
virtual void operator()();
virtual ~Thread();
};
include <iostream>
Thread::Thread() : ThreadID(*this) {
}
void Thread::operator()() {
std::cerr << "NOWORK" << std::endl;
}
And an actual implementation:
Thread_Server::Thread_Server() : Thread() {
}
void Thread_Server::operator()() {
std::cerr << "working" << std::endl;
while (1) {
std::cerr << "run" << std::endl;
sleep(3);
}
}
This compiles and works. However the derived classes operator() is never
called, only the base classes. This means when I start a thread:
Thread_Server x = Thread_Server();
it only outputs "NOWORK" and terminates. However, as the Thread_Server()
constructor calls the Thread() constructor which creates the actual
boost::thread with *this I assumed that it would make a lookup in the
vtable (virtual operator()!) and start the derived classes working function.
It does not, however. It is my assumption that during the dereferencing
step of *this the Derived class somehow gets lost - but I do not
understnad why exactly.
Trying to follow your advice using pointers, I also tried:
Thread_Server::Thread_Server() : Thread(this) {
}
With a new constructor in Thread():
Thread::Thread(const Thread *Derived) : ThreadID(*Derived) {
}
Which yielded exactly the same result.
Could you elaborate on why this happens?
Kind regards,
Johannes
--
"Meine Gegenklage gegen dich lautet dann auf bewusste Verlogenheit,
verl??sterung von Gott, Bibel und mir und bewusster Blasphemie."
-- Prophet und Vision??r Hans Joss aka HJP in de.sci.physik
<48d8bf1d$0$7510$5402220f@news.sunrise.ch>