Re: Desperation with Boost Threads
Johannes Bauer wrote:
Hello group,
I strongly hope this is on-topic here - I guess it's a language question
as Boost is more or less part of the C++ language (or at least it is a
candidate for getting into the standard). If I'm wrong here, please
point me to the correct location.
Well, it depends on which part of boost. The boost mailing lists are
more appropriate for most boost libraries. However, boost::thread would
be on-topic here since it is the API (more or less) that will be in the
next standard.
Basically I'm just trying to get a incredibly simple, stupid example to
work - and I can't. It doesn't compile:
#include <boost/thread.hpp>
class Thread {
private:
boost::thread ThreadID;
void operator()();
public:
Thread();
void Run();
virtual void Work() = 0;
virtual ~Thread();
};
Thread::Thread() {
}
You need to initialize ThreadID here.
void Thread::operator()() {
Work();
}
void Thread::Run() {
ThreadID = boost::thread(*this);
You can't do this. Threads are not "copyable" and thus not assignable.
}
Thread::~Thread() {
}
I get a bunch of compiler errors that I'm not even close to understanding:
g++ -O2 -Wall -g -DTRACE -ICommon -c -o Thread.o Thread.cpp
/usr/include/boost/noncopyable.hpp: In copy constructor
???boost::thread::thread(const boost::thread&)???:
/usr/include/boost/noncopyable.hpp:27: error:
???boost::noncopyable_::noncopyable::noncopyable(const
boost::noncopyable_::noncopyable&)??? is private
boost::noncopyable is meant to tell you that you can't copy this object
and are not meant to be able to.
/usr/include/boost/thread/thread.hpp:35: error: within this context
Thread.hpp: In copy constructor ???Thread::Thread(const Thread&)???:
Thread.hpp:6: note: synthesized method ???boost::thread::thread(const
boost::thread&)??? first required here
Thread.cpp: In member function ???void Thread::Run()???:
You explicitly tried to copy a noncopyable object here.
Thread.cpp:11: note: synthesized method ???Thread::Thread(const Thread&)???
first required here
The compiler generated a copy method that attempts to copy a noncopyable.
Thread.cpp:11: error: initializing argument 1 of ???boost::function0<R,
Allocator>::function0(Functor, typename
boost::enable_if_c<boost::type_traits::ice_not<boost::is_integral<Functor>::value>::value,
int>::type) [with Functor = Thread, R = void, Allocator =
std::allocator<boost::function_base>]???
Thread.cpp:11: error: cannot allocate an object of abstract type ???Thread???
Thread.hpp:6: note: because the following virtual functions are pure
within ???Thread???:
Thread.hpp:13: note: virtual void Thread::Work()
Thread is abstract because you said = 0 in the declaration. Abstract
functions can have bodies but you are required to implement a subclass
that provides that function (in non-abstract form) that either overrides
super's behavior or calls it.
/usr/include/boost/noncopyable.hpp: In member function ???boost::thread&
boost::thread::operator=(const boost::thread&)???:
/usr/include/boost/noncopyable.hpp:28: error: ???const
boost::noncopyable_::noncopyable&
boost::noncopyable_::noncopyable::operator=(const
boost::noncopyable_::noncopyable&)??? is private
/usr/include/boost/thread/thread.hpp:35: error: within this context
You're compiler generated an assignment operator that's also hosed for
the same reasons as the rest.
[...]
What I want should be pretty clear: I want to be able to derive a class
from Thread to use it in such a fashion:
public mythread : public Thread {
mythread() : Thread() { }
void Work() {
while (true)
std::cerr << "Working..." << std::endl;
}
}
and then use it:
Thread x = mythread();
x.Run();
I want to use that abstraction so I can later on provide more
functionality (such as x.Abort() or such).
Can you tell me how to get that piece of code to compile? I'm desperate
after about two hours of fiddling with three lines of code :-\
Stop trying to copy threads.
Kind regards,
Johannes