开发者

forward declaration of boost::thread variable

开发者 https://www.devze.com 2023-02-01 20:32 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号