synchronize 1.1.1 copy "synchronize: ^1.1.1" to clipboard
synchronize: ^1.1.1 copied to clipboard

This package provides synchronization implementations, including a (reentrant) Lock, a (upgradable) ReaderWriterLock, and a Semaphore.

Synchronize #

This package provides various synchronization implementations:

  • Lock for exclusive access This lock can be used to execute critical sections of code. The provided implementation supports reentrancy.

  • ReaderWriterLock for single writer/multiple reader synchronization The provided implementation does not supports reentrancy, but locks can be upgraded and/or downgraded.

  • Semaphore for controlling access to resources.

Implementations are based on package using and support cancelation tokens as well as automatic releasing via use() / useAsync() / execute() / executeAsync().

Example #

void main() {

  final cache = <String, Data>{};

  Future<Data?> fromCache(String key) =>
    ReaderWriterLock.read(cache).useAsync((reader) async {

      // multiple readers may retrieve some data from the cache
      if (cache.containsKey(key)) {
         return cache[key]!;
      }

      // when the data is not in cache, the lock is upgraded
      return reader.upgrade().useAsync((writer) async {
        // we now have exclusive access to load or compute data and update
        // the cache

        // ... load data, update cache ...
        return data;
      })
    });

}
4
likes
160
points
1.58k
downloads

Publisher

unverified uploader

Weekly Downloads

This package provides synchronization implementations, including a (reentrant) Lock, a (upgradable) ReaderWriterLock, and a Semaphore.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

cancelation_token, using

More

Packages that depend on synchronize