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

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

Hello,

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!

Ray

 

2 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

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?

-Nikita

Leave a Comment

Please sign in to add a comment. Not a member? Join today