While reading the book c++ concurrency in action,I'm trying to write a thread-safe queue.
The code:
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_;
};
My question is whether should I lock the argument's other.mutex_
in the move constructor? Why?