数据互换、比较显然必须并发访问两个对象,一般该操作需要锁定两个互斥。

class Y {
private:
    int some_details;
    mutable std::mutex mtx;
    int get_details() const {
        std::lock_guard<std::mutex> guard(mtx);
        return some_details;
    }

public:
    Y(int sd) : some_details(sd) {}
    friend bool operator==(Y const& lhs, Y const& rhs) {
        if (&lhs == &rhs) return true;
        int const lhs_val = lhs.get_details();
        int const rhs_val = rhs.get_details();
        return rhs_val == lhs_val;
    }
};