lock method
Implementation
Future<void> lock(Future<void> Function() criticalSection) async { // Changed to Future<void> Function()
while (_completer != null) {
await _completer!.future;
}
_completer = Completer<void>();
try {
await criticalSection();
} finally {
if (_completer != null && !_completer!.isCompleted) {
_completer!.complete();
}
_completer = null;
}
}