Skip to content

Simple but the best Mutex providing a exclusive write lock and shared read-only locks.

License

Notifications You must be signed in to change notification settings

Cat-sushi/simple_mutex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Mutex

This provids a exclusive write lock and shared read-only locks.

Request for exclusive lock can gracefully interrupt multiple parallel loops acquiring shared locks.

Features

  • Literally mutually exclusive lock, for read/ write user of resources.
  • Shared locks, for read-only users.
  • Eclusive critical section helper with retrun value.
  • Shared critical section helper with return value.

Getting started

import 'simple_mutex/simple_mutex.dart';

Usage

Declaration.

final mutex = Mutex();

Protect asynchronous critical section with mutually exclusive lock.

await mutex.lock();
try {
  // Some mutually exclusive asynchronous critical section.
  // This prevent entering other mutually exclusive/ shared critical sections.
} finally {
  mutex.unlock();
}

Protect asynchronous critical section with shared lock.

await mutex.lockShared();
try {
  // Some shared asynchronous critical section.
  // This prevent entering other mutually exclusive critical sections.
  // On the other hand, this can be run in parallel with other shared 
  // critical sections.
} finally {
  mutex.unlockShared();
}

To avoid leaking lock in exceptional cases, critical and criticalShared are recommended.

Lint unawaited_futures is also recommended, because if you miss await for critical or criticalShared, memory would be exhausted.

About

Simple but the best Mutex providing a exclusive write lock and shared read-only locks.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages