Forward declaration boost :: thread variable

can someone please tell me if we can forward the boost :: thread declaration. boost :: thread t (thread); starts the thread, but I want to declare it somewhere and run it elsewhere. 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 
+5
source share
1 answer

As far as I know, the only way to do this is to use thread 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 like you can't use move semantics with your version of boost. In this case, I'm afraid you cannot initialize the instance threadand start it later.

+4

All Articles