Crate closeable_mutex

source ·
Expand description

Provides a mutex that can be closed for long-term access.

This is useful if you have an object that is in one of two states: a concurrent state, where it can be accessed by multiple users, and a non-concurrent state, where it can only be accessed by one user.

In the non-concurrent state, you can close the mutex guarding the object so that it can be accessed freely without additional locking, allowing it to be used in async functions (for example). When the object is to reenter the concurrent state, you can open the mutex, allowing normal mutex operations.

Something similar to this can be achieved with an ordinary mutex by holding the lock for the lifetime of the non-concurrent state, but this means that any other attempt to lock the mutex will hang for an indefinite period of time, possibly deadlocking. try_lock cannot be used to overcome this, because it would also fail while in the concurrent state with multiple concurrent accessors competing for the lock.

Structs§