在下面的代码中,对象“队列”是不可复制的,但由于 std::mutex 是可移动的。
std::generate_n(std::back_inserter(thread_pool),
std::thread::hardware_concurrency,
[&](){return std::thread(handler(), exiting, queue);});
VC++2012 由于互斥量上的私有(private)复制构造函数而无法编译。无法为队列生成复制构造函数。为什么会有任何东西试图复制队列?在我看来,一切都是引用,因此没有拷贝。
最佳答案
您 正在尝试通过将queue
按值传递给std::thread
构造函数来复制它。如果您打算传递引用,请使用包装器:std::ref(queue)
。
如果你真的想将queue
移动到std::thread
中,你需要将std::move(queue)
传递给使它成为一个右值。它仍然无法正常工作,because of a bug in VS .
关于c++ - 仅移动类型的 back_inserter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18152817/