Is it okay to use Mutex<bool>?

Ah, so when you wrote

then you meant the first operation on txn2 must happen-before the last operation on txn1?

I think I understand now. (Please let me know if I got it wrong.)

So in practice it actually boils down to whether we can assume that MDB_txn * pointers are unique. While the LMDB API doesn't guarantee it (and in theory, a write transaction could have the same address as an arbitrary read transaction), I understand that it may be reasonable to assume it nonetheless.

Concluding (getting back to the subject of this thread), the Mutex<bool> could – in this particular case – probably be replaced with an AtomicBool using Relaxed loads and stores. However, it requires some extra assumptions being true in regard LMDB behaving reasonably (which is not explicitly documented in the specification, and which might be similar with many other C APIs).

Replacing the Mutex<Bool> with an atomic that uses simple loads and stores with SeqCst still requires these extra assumptions to be true. So using SeqCst makes no sense (here), which also matches what @farnz quoted in the other thread:


Seeing this from the other side: If a Mutex<bool> was necessary in this case, the solution to replace it with atomics is really complex and requires a lot of thinking and reasoning, and it doesn't result in simple stores and loads from an atomic.

1 Like