user1382306
user1382306

Reputation:

How to configure as a non-blocking unique_lock?

I have found in the docs that a unique_lock can try "to acquire the lock in a non-blocking fashion".

Is it the unique_lock's thread that isn't blocked as it tries to acquire the lock in the "non-blocking fashion"? If so, does it simply fail gracefully? Will unlock?

Also, since notify_one doesn't throw, does that mean that if it cannot notify, it will also fail gracefully?

If the unique_lock can be set not to wait for a successful lock but fail gracefully and move on with graceful failures for unlock and notify_one, how can a unique_lock be set this way?

Upvotes: 0

Views: 226

Answers (1)

Mark B
Mark B

Reputation: 96233

Something like boost::unique_lock lock(my_lockable, boost::try_to_lock); should do the trick.

lock.owns_lock() will return true if the lock were acquired. If it wasn't acquired you should not attempt to release it.

It should always be safe to call notify_one even if no condition is waiting for it.

Upvotes: 1

Related Questions