gpt4 book ai didi

c++ - boost::thread 变量的前向声明

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:36:52 25 4
gpt4 key购买 nike

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

当我使用

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

最佳答案

据我所知,唯一的方法是使用threadmove 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`.

编辑:从您发布的错误消息来看,您似乎无法在您的 boost 版本中使用移动语义。如果是这样的话,恐怕您将无法初始化 thread 实例并让它稍后启动。

关于c++ - boost::thread 变量的前向声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4553520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com