【发布时间】:2016-05-11 19:50:35
【问题描述】:
class A
{
private:
class B
{
private:
std::mutex mu;
A* parent = NULL;
public:
B(A* const parent_ptr): parent(parent_ptr) {}
B(const A::B & b_copy) { /* I thought I needed code here */ }
};
public:
B b = B(this); //...to make this copy instruction work.
// (Copy constructor is deleted, need to declare a new one?)
};
我有一个类B,它基本上是一个线程安全的任务队列。它包含一个deque、一个mutex 和一个condition_variable。它促进了由类A 启动的任意两个线程之间的消费者/生产者关系。我已经尽可能地简化了代码。
问题始于将mutex 作为成员:这会删除默认的复制构造函数。这只是意味着我可以使用B(this) 构造,但我无法使用B b = B(this) 构造和 副本,这是我在最后一行中需要做的,以便给A 上课班级B 的成员。解决此问题的最佳方法是什么?
【问题讨论】:
标签: class mutex copy-constructor member move-constructor