Assignment via copy-and-swap vs two locks
问题 Borrowing Howard Hinnant's example and modifying it to use copy-and-swap, is this op= thread-safe? struct A { A() = default; A(A const &x); // Assume implements correct locking and copying. A& operator=(A x) { std::lock_guard<std::mutex> lock_data (_mut); using std::swap; swap(_data, x._data); return *this; } private: mutable std::mutex _mut; std::vector<double> _data; }; I believe this thread-safe (remember op='s parameter is passed by value), and the only problem I can find is the one swept