forward declaration of boost::thread variable
can someone please tell me can we forward declare a boost::thread variable. boost::thread t(thread); starts a thread, 开发者_开发问答but I want to declare it somewhere and to start it somewhere else. Thanx in advance.
When I use
boost::thread t;
t=boost::thread (thread);
/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
/usr/include/boost/thread/thread.hpp:35: error: within this context
thr.cpp: In function ‘int main()’:
thr.cpp:20: note: synthesized method ‘boost::thread::thread(const boost::thread&)’ first required here
/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
thr.cpp: In function ‘int main()’:
thr.cpp:20: note: synthesized method ‘boost::thread& boost::thread::operator=(const boost::thread&)’ first required here
To my knowledge, the only way to do that is to use thread
's move semantics:
boost::thread t; // Will be initialized to `Not-a-Thread`.
// Later...
t = boost::thread(your_callable);
// Now `your_callable()` runs inside a new thread that has been moved to `t`.
EDIT: From the error messages you posted, it seems that you can't use move semantics with your version of boost. If that's the case, I'm afraid you won't be able to initialize a thread
instance and have it start later.
精彩评论