How can I know whether a mutex is locked or free?

How can I know whether a mutex is locked or free?


I am implementing a function that the thread need to know the state of the mutex(i.e. locked or free). But I find that TBB mutex does NOT provide such a function interface. 

There is a function named "bool M::scoped_lock::try_acquire(M&) " providing a similar action but it can acquire the lock on it if the mutex is free. I just wanna know the state of the mutex rather than acquire the lock on the mutex if it's free.

Can someone help me? How can I achieve my goal? Thanks!



publicaciones de 2 / 0 nuevos
Último envío
Para obtener más información sobre las optimizaciones del compilador, consulte el aviso sobre la optimización.

Hi Ray!

Generally, you can't reasonably check whether a mutex is locked, because of the race between checking the "state" of the mutex and possible locking/unlocking by another thread. Maybe you could just introduce a boolean flag after mutex::lock/unlock() calls and check when necessary. But still "M:try_lock()" seems to be more reliable. Please, provide the details of your situation, maybe there is another solution for your problem?


Deje un comentario

Por favor inicie sesión para agregar un comentario. ¿No es socio? Únase ya