Implementing swap for class with std::mutex

前端 未结 2 1815
春和景丽
春和景丽 2021-02-07 17:10

Suppose we have a class with a std::mutex:

class Foo
{
    std::mutex mutex_;
    std::string str_;
    // other members etc
public:
           


        
2条回答
  •  北海茫月
    2021-02-07 18:08

    I don't think your swap implementation is safe. If another algorithm tries to lock rhs.mutex_ first and then lhs.mutex_, you may end up with a deadlock. Try std::lock() instead.

提交回复
热议问题