How to assert if a std::mutex is locked?

前端 未结 7 1556
难免孤独
难免孤独 2020-12-03 13:30

With GCC 4.8.2 (on Linux/Debian/Sid 64 bits) -or GCC 4.9 when available - in C++11- I have some mutex

std::mutex gmtx;

actually, it is

7条回答
  •  离开以前
    2020-12-03 13:53

    My solution is simple, use try_lock to test then unlock if needed:

    std::mutex mtx;
    
    bool is_locked() {
       if (mtx.try_lock()) {
          mtx.unlock();
          return false;
       }
       return true; // locked thus try_lock failed
    }
    

提交回复
热议问题