mutex 1.0.1

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 86

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;
synchronousOperations();
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 {
  m.release();
}

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 {
  m.release();
}

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 {
  m.release();
}

Features and bugs

Please file feature requests and bugs at the issue tracker.

Changelog

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:


dependencies:
  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
Popularity:
Describes how popular the package is relative to other packages. [more]
72
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
86
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

Platforms

Detected platforms: Flutter, web, other

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

Suggestions

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.

Dependencies

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