Skip to content
pubscout

synchronized

3.6.1+1, released 11 days ago by tekartik.com

Lock mechanism to prevent concurrent access to asynchronous code.

WASM-readyRock solid
$ flutter pub add synchronized
Source repository

What it does

Provides a basic Lock, ReentrantLock, MultiLock, and GlobalLock to ensure that critical sections of asynchronous operations run sequentially within a single isolate or globally across processes. Preserves return values and handles timeouts when acquiring locks.

AI-written summary of version 3.6.1, based on the package's README and metadata. Report an inaccuracy

Use it when

  • You need to provide transactions on database systems lacking built-in transaction mechanisms like MongoDB or file systems
  • You want to ensure asynchronous UI operations in a web app do not conflict
  • You need cross-isolate, cross-process, or cross-browser tab synchronization using GlobalLock

Skip it when

  • You need cross-process synchronization and are running on node (where GlobalLock is limited to the current process)

Strengths

  • No dependencies other than the Dart SDK
  • Supports browser, DartVM, and Flutter across multiple platforms
  • Offers reentrant lock options via Zones and multi-lock capabilities

Weaknesses

  • The name is biased since Dart is single-threaded; it only helps handle asynchronous await interleaving
  • Basic locks are not reentrant by default

Setup

Kind
Pure Dart
Effort
Low
Code generation
No
Direct dependencies
0
import 'package:synchronized/synchronized.dart';

Future<void> main() async {
  var lock = Lock();
  await lock.synchronized(() async {
    // Only this block can run (once) until done
  });
}

Alternatives

Packages that do the same job and could replace synchronized.

  • async_locks

    Synchronization primitives for asynchronous Dart code like Lock, Event, and Semaphore

  • rx

    Reactive Extensions library for Dart offering observable streams and async operators.

  • queue_it

    A Dart queue with concurrency, retries and event listeners

  • http_status_code

    A Dart library providing constants and helpers for HTTP status codes.

Works with

Packages commonly used together with synchronized: ones it builds on, ones built on it, and its own family.