protectWrite method
Convenience method for protecting a function with a write lock.
A write lock is acquired before invoking the criticalSection
function.
If the critical section returns a Future, it waits for it to be completed
before the write lock is released. The write lock is always released
(even if the critical section throws an exception).
Returns a Future that completes after the write lock is released.
Implementation
Future<void> protectWrite(Function criticalSection) async {
await acquireWrite();
try {
await criticalSection();
} finally {
release();
}
}