### MultiLock Synchronization Example Source: https://github.com/tekartik/synchronized.dart/blob/master/synchronized/README.md Demonstrates how to use MultiLock to synchronize access to multiple locks at once. Ensure that the locks are initialized before creating the MultiLock. ```dart var lock1 = Lock(); var lock2 = Lock(); var multiLock = MultiLock(locks: [lock1, lock2]); multiLock.synchronized(() async { // lock1 and lock2 are locked at this point ... }); ``` -------------------------------- ### Preventing Concurrent Execution Example Source: https://github.com/tekartik/synchronized.dart/blob/master/synchronized/README.md Demonstrates how synchronized prevents interleaved execution of asynchronous tasks. Without synchronization, calls to write1234() would interleave their output; with synchronization, they execute sequentially. ```dart Future writeSlow(int value) async { await Future.delayed(new Duration(milliseconds: 1)); stdout.write(value); } Future write(List values) async { for (int value in values) { await writeSlow(value); } } Future write1234() async { await write([1, 2, 3, 4]); } // Without synchronization: // write1234(); // write1234(); // would print: 11223344 // With synchronization: // lock.synchronized(write1234); // lock.synchronized(write1234); // would print: 12341234 ``` -------------------------------- ### Publish Package Source: https://github.com/tekartik/synchronized.dart/blob/master/synchronized/doc/synchronized_development.md Command to publish the package to pub.dev. ```bash pub publish ``` -------------------------------- ### Run Quick Checks Before Committing Source: https://github.com/tekartik/synchronized.dart/blob/master/synchronized/doc/synchronized_development.md Execute dartfmt for code formatting, dartanalyzer for static analysis, and pub run test for running unit tests before committing changes. ```bash dartfmt -w . dartanalyzer . pub run test ``` -------------------------------- ### Tag and Push Release Source: https://github.com/tekartik/synchronized.dart/blob/master/synchronized/doc/synchronized_development.md After publishing, create a Git tag for the release version and push the tags to the remote repository. ```bash git tag vX.Y.Z git push origin --tags ``` -------------------------------- ### Run Performance Tests Source: https://github.com/tekartik/synchronized.dart/blob/master/synchronized/doc/synchronized_development.md Execute performance tests using 'pub run test' with the '-j 1' flag to limit concurrency and specify the performance test file. ```bash pub run test -j 1 test/perf_test_.dart ``` -------------------------------- ### Run Browser and Node.js Tests Source: https://github.com/tekartik/synchronized.dart/blob/master/synchronized/doc/synchronized_development.md Run tests specifically in a Chrome browser, or execute a full test suite across Chrome, Firefox, and the Node.js virtual machine (VM). ```bash pub run test -p chrome # full test in one pub run test -p chrome -p firefox -p vm # Using build_runner pub run build_runner test -- -p chrome -p firefox -p vm ``` -------------------------------- ### Object Extension Synchronization Source: https://github.com/tekartik/synchronized.dart/blob/master/synchronized/doc/how_to.md Import the extension to add a synchronized method to any object, allowing for instance-level synchronization without explicitly managing a Lock instance. ```dart import 'package:synchronized/extension.dart'; ``` ```dart myObject.synchronized(() async { // ...uninterrupted action }); ``` ```dart class MyClass { /// Perform a long action that won't be called more than once at a time. /// Future performAction() { // Lock at the instance level return synchronized(() async { // ...uninterrupted action }); } } ``` -------------------------------- ### Basic Lock Usage Source: https://github.com/tekartik/synchronized.dart/blob/master/synchronized/doc/how_to.md Instantiate a Lock and use it to synchronize asynchronous operations. The same Lock instance must be shared across calls to ensure effective concurrency control. ```dart var lock = new Lock(); // ... await lock.synchronized(() async { // do you stuff // await ... }); ``` -------------------------------- ### Object as a Lock (Extension) Source: https://github.com/tekartik/synchronized.dart/blob/master/synchronized/README.md Use the synchronized extension from 'package:synchronized/extension.dart' to turn any object into a lock. This allows calling synchronized() directly on an object instance. ```dart import 'package:synchronized/extension.dart'; class MyClass { /// Perform a long action that won't be called more than one at a time. Future performAction() { // Lock at the instance level return synchronized(() async { // ...uninterrupted action }); } } ``` -------------------------------- ### Use Git Version of Synchronized.dart Source: https://github.com/tekartik/synchronized.dart/blob/master/synchronized/doc/synchronized_development.md Override the dependency in your pubspec.yaml to use the latest version of synchronized.dart directly from its Git repository. ```yaml dependency_overrides: synchronized: git: https://github.com/tekartik/synchronized.dart ``` -------------------------------- ### Class-Level Synchronization via Extension Source: https://github.com/tekartik/synchronized.dart/blob/master/synchronized/doc/how_to.md Synchronize operations at the class level by calling the synchronized method on the runtime type of the class. This ensures that only one instance of the class can execute the synchronized block at a time. ```dart class MyClass { /// Perform a long action that won't be called more than once at a time. Future performClassAction() { // Lock at the class level return runtimeType.synchronized(() async { // ...uninterrupted action }); } } ``` -------------------------------- ### Class Instance Lock Source: https://github.com/tekartik/synchronized.dart/blob/master/synchronized/README.md Create a Lock instance within a class to protect the class's instance data and resources from concurrent modifications by different method calls on the same instance. ```dart class MyClass { final _lock = new Lock(); Future myMethod() async { await _lock.synchronized(() async { step1(); step2(); step3(); }); } } ``` -------------------------------- ### Basic Lock (Default Behavior) Source: https://github.com/tekartik/synchronized.dart/blob/master/synchronized/README.md A basic lock is not reentrant by default and does not use Zone. It functions as an asynchronous executor with a pool capacity of 1. ```dart var lock = Lock(); // ... lock.synchronized(() async { // do some stuff // ... }); ``` -------------------------------- ### Reentrant Lock Usage Source: https://github.com/tekartik/synchronized.dart/blob/master/synchronized/README.md Create a Lock with reentrant: true to allow nested synchronized calls within the same zone. This is useful when a function that acquires a lock might call another function that also needs to acquire the same lock. ```dart var lock = new Lock(reentrant: true); // ... await lock.synchronized(() async { // do some stuff // ... await lock.synchronized(() async { // other stuff } }); ``` -------------------------------- ### Class-Level Synchronization Source: https://github.com/tekartik/synchronized.dart/blob/master/synchronized/doc/how_to.md Use a Lock instance within a class to ensure that methods requiring exclusive access are not called concurrently on the same object instance. ```dart class MyClass { Lock _lock = new Lock(); Future myMethod() async { await _lock.synchronized(() async { step1(); step2(); step3(); }); } } ``` -------------------------------- ### Preserving Return Values Source: https://github.com/tekartik/synchronized.dart/blob/master/synchronized/README.md The synchronized method preserves and returns the value or error from the synchronized block. ```dart int value = await lock.synchronized(() { return 1; }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.