mutex 1.0.2

  • Readme
  • Changelog
  • Installing
  • 89

mutex #

A library for mutual exclusion.

Purpose #

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.

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.

Changelog #

1.0.2 #

  • Code clean up to satisfy pana 0.13.2 health checks.

1.0.1 #

  • Fixed dartanalyzer warnings.

1.0.0 #

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

0.0.1 #

  • Initial version

Use this package as a library

1. Depend on it

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

  mutex: ^1.0.2

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub 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';
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 Jan 19, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Maintenance suggestions

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and mutex.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.


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