mutex 0.0.1

  • Installing
  • Versions
  • 69


A library for mutual exclusion.


Mutexes can be used to protect critical sections of code to prevent race conditions.

Although Dart uses a single thread of execution, race conditions can still occur when asynchronous operations are used inside critical sections. For example,

x = 42;
assert(x == 42); // x will not have changed

y = 42;
await asynchronousOperations();
assert(y == 42 || y != 42); // y might have changed

An example is when Dart is used to implement a server-side Web server that updates a database. The update involves querying the database, performing some calculations, and then updating the database; and you don't want the database to be changed by something else before you are finished updating it. That something else could be the same Web server handling another request in parallel.

This package provides a normal mutex and a read-write mutex.


A mutex guarantees only one lock can be acquired at any one time.

import 'package:mutex/mutex.dart';

m = new Mutex();

await m.acquire();
try {
  // critical section
finally {

Read-write mutex

A read-write mutex allow multiple reads locks to be acquired at the same time, but all at most one write lock can be acquired at the same time.

import 'package:mutex/mutex.dart';

m = new MutexReadWrite();

Acquiring a write lock:

await m.acquireWrite();
try {
  // critical write section
  // No other locks (read or write) can be acquired.
finally {

Acquiring a read lock:

await m.acquireRead();
try {
  // critical read section
  // No write locks can be acquired, but other read locks can be acquired.
finally {

Features and bugs

Please file feature requests and bugs at the issue tracker.



  • Initial version

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  mutex: "^0.0.1"

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

      import 'package:mutex/mutex.dart';
Version Uploaded Documentation Archive
0.0.1 Nov 11, 2016 Go to the documentation of mutex 0.0.1 Download mutex 0.0.1 archive


We analyzed this package on Jun 12, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.60.0
  • pana: 0.11.3


Describes how popular the package is relative to other packages. [more]
62 / 100
Code health derived from static analysis. [more]
99 / 100
Reflects how tidy and up-to-date the package is. [more]
41 / 100
Overall score:
Weighted score of the above. [more]
Learn more about scoring.


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:mutex/mutex.dart.


  • Package is getting outdated.

    The package was released 82 weeks ago.

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Maintain an example.

    Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use mutex.dart.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 1 hint.

    Run dartfmt to format lib/src/read_write_mutex.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.0.0 <2.0.0
Dev dependencies
test >=0.12.0 <0.13.0