mutex 1.0.1

  • Installing
  • Versions
  • 86


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.



  • Fixed dartanalyzer warnings.


  • Updated the upper bound of the SDK constraint to <3.0.0.


  • Initial version

Use this package as a library

1. Depend on it

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

  mutex: ^1.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
1.0.1 Aug 10, 2018 Go to the documentation of mutex 1.0.1 Download mutex 1.0.1 archive
1.0.0 Aug 8, 2018 Go to the documentation of mutex 1.0.0 Download mutex 1.0.0 archive
0.0.1 Nov 11, 2016 Go to the documentation of mutex 0.0.1 Download mutex 0.0.1 archive
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

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

  • Dart: 2.0.0
  • pana: 0.11.8


Detected platforms: Flutter, web, other

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


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.

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.


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