boost :: thread变量的前向声明

时间:2010-12-29 11:05:18

标签: c++ boost

有人可以告诉我,我们可以转发声明一个boost :: thread变量。 boost :: thread t(thread);启动一个线程,但我想在某个地方声明它并在其他地方启动它。 Thanx提前。

当我使用

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 

1 个答案:

答案 0 :(得分:4)

据我所知,唯一的方法是使用thread的{​​{3}}:

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`.

编辑:根据您发布的错误消息,您似乎无法在您的版本的boost中使用移动语义。如果是这种情况,我担心您将无法初始化thread实例并让它在稍后启动。