【发布时间】:2014-11-21 23:06:45
【问题描述】:
在阅读c++ concurrency in action这本书时,我正在尝试编写一个线程安全队列。
代码:
template<typename T>
class ThreadsafeQueue
{
public:
using Guard = std::lock_guard<std::mutex>;
//! default Ctor
ThreadsafeQueue() = default;
//! copy Ctor
ThreadsafeQueue(ThreadsafeQueue const& other)
{
Guard g{other.mutex_};
q_ = other.q_;
}
//! move Ctor <----my question
ThreadsafeQueue(ThreadsafeQueue && other)noexcept
{
q_ = std::move(other.q_);
}
//! other members...
private:
std::queue<T> q_;
std::mutex mutex_;
std::condition_variable cond_;
};
我的问题是我是否应该在移动构造函数中锁定参数的other.mutex_?为什么?
【问题讨论】:
标签: c++ multithreading c++11 thread-safety move-semantics