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

前端 未结 7 1543
难免孤独
难免孤独 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 14:04

    Well, if the expense of the assertion really isn't an issue then you can just call try_lock() from another thread where its behavior is guaranteed to be well defined:

    void beta(void) {
      assert(std::async(std::launch::async, [] { return gmtx.try_lock(); })
                     .get() == false &&
             "error, beta called without locking gmtx");
      // some real work
    }
    
    0 讨论(0)
提交回复
热议问题