dmutex
is a Dart package that provides a smart locking mechanism to control concurrency in your Dart applications. It helps prevent chaotic execution and ensures orderly processing of tasks in a single-threaded environment.
In a single-threaded programming world like Dart, managing concurrent execution can still be challenging, especially when dealing with asynchronous tasks. dmutex
comes to the rescue by providing a specialized locking mechanism known as a "mutex" (short for "mutual exclusion"). Just like a special key ensures only one person (or "task") is allowed to access a particular resource at a time, dmutex
helps ensure that only one task can access a critical section at a time.
dmutex
helps you maintain order and prevent conflicts when working with asynchronous tasks, even in a single-threaded environment like Dart.
- Provides a smart locking mechanism for controlling concurrency.
- Prevents multiple asynchronous tasks from accessing critical sections simultaneously.
- Offers both basic locking (
DLock
) and reentrant locking (ReentrantDLock
). - Helps maintain execution order and synchronization.
- Easily integrates with your Dart applications.
Add this to your pubspec.yaml
:
dependencies:
dmutex: ^1.0.0
Then, run dart pub get
to install the package.
Here's how you can use the classes provided by this package.
An asynchronous lock implementation that provides exclusive access control.
import 'package:dmutex/dmutex.dart';
void main() async {
final dLock = DLock();
await dLock.withLock(() async {
// Critical section
});
}
A reentrant lock that allows nested synchronized blocks.
import 'package:dmutex/dmutex.dart';
void main() async {
final reentrantDLock = ReentrantDLock();
await reentrantDLock.withLock(() async {
// Outer synchronized block
await reentrantDLock.withLock(() async {
// Inner synchronized block
});
});
}
For more examples, check out the example directory.
To run the tests for this package, use the following command:
dart test
This project is licensed under the MIT License - see the LICENSE file for details.
This is a basic overview of dmutex
. For more detailed information and advanced usage, refer to the package documentation on pub.dev.
Maintained with ❤️ by olololoe110399