template<typename T>
class TSQueue {
public:
TSQueue() {}
TSQueue(const TSQueue& rhs) {
lock_guard<mutex> lg1(rhs._mutex);
_data = rhs._data;
}
private:
queue<shared_ptr<T> > _data;
mutex _mutex;
};
I have seen in textbooks that only source (rhs) is locked in copy constructor. I am not sure how exclusive access of destination (this) is ensured by only locking rhs. I think both source and destination should be locked. What is the ideal way to write thread safe copy constructor?