Skip to content
pubscout

mutex

3.1.0, released 3 years ago by hoylen.com

Mutual exclusion locks and read-write locks for asynchronous Dart code

WASM-ready
$ flutter pub add mutex
Source repository

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.

  • pubspec_manager

    Type-safe API to read, modify, and write pubspec.yaml while preserving comments.

  • json_path

    Dart implementation of RFC 9535 JSONPath for querying and manipulating JSON

  • rfc_6901

    Implements RFC 6901 JSON Pointer for reading, writing and removing values in JSON documents.

Works with

Packages commonly used together with mutex: ones it builds on, ones built on it, and its own family.