mutex
3.1.0, released 3 years ago by hoylen.com
Mutual exclusion locks and read-write locks for asynchronous Dart code
What it does
Provides normal mutex and read-write mutex implementations to protect critical sections of code and prevent race conditions when using asynchronous operations. Includes convenience methods like protect, protectRead, and protectWrite to safely acquire and release locks.
AI-written summary of version 3.1.0, based on the package's README and metadata. Report an inaccuracy
Use it when
- You have asynchronous operations inside critical sections that risk race conditions
- You need multiple read locks to exist simultaneously while restricting write locks to a single exclusive instance
Skip it when
- Your critical section only contains synchronous code, which cannot be interrupted in Dart
- Your critical section does not involve shared data or resources accessible by other asynchronous code
Strengths
- Supports both normal mutexes and read-write mutexes
- Convenience methods ensure locks are always released correctly via try-finally blocks
Weaknesses
- Requires proper management to avoid deadlocks in complex asynchronous flows
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- No
- Direct dependencies
- 0
final m = Mutex();
await m.protect(() async {
// critical section
});Alternatives
Packages that do the same job and could replace mutex.
- dartchess
A Dart library for chess rules, move generation, and FEN/PGN parsing with variant support.
81Compare - pubspec_manager
Type-safe API to read, modify, and write pubspec.yaml while preserving comments.
78Compare - json_path
Dart implementation of RFC 9535 JSONPath for querying and manipulating JSON
65Compare - rfc_6901
Implements RFC 6901 JSON Pointer for reading, writing and removing values in JSON documents.
61Compare
Works with
Packages commonly used together with mutex: ones it builds on, ones built on it, and its own family.
- flutter_edge_aiBuilt on mutexOn-device AI for Flutter to run Gemma and other LLMs, embeddings, and speech without a server.
- at_lookupBuilt on mutexCore commands implementation for communicating with an Atsign secondary server.
- at_clientBuilt on mutexClient SDK for building apps on the atProtocol with end-to-end encryption.
- flutter_edge_ai_litertlmBuilt on mutexLiteRT-LM on-device engine for flutter_edge_ai supporting .litertlm models and embeddings.
- json_cacheBuilt on mutexUnifies local storage packages under a single object-oriented JSON caching interface.
- stockBuilt on mutexDart package for async data loading, caching, and combining local and network data sources.
- countdown_widgetBuilt on mutexA timer widget that continues to work correctly even when the app is locked.