tbachert/sync

Synchronization primitives

v1.0.0 2022-12-10 14:31 UTC

This package is auto-updated.

Last update: 2024-11-03 23:49:00 UTC


README

Installation

composer require tbachert/sync

Usage

Example

$example = new Example();
await([
    async($example->increment(...), 5),
    async($example->increment(...), 4),
    async(fn() => var_dump($example->get())),
]);
var_dump($example->get());
// int(0)
// int(4)

$example = new ExampleSynchronized();
await([
    async($example->increment(...), 5),
    async($example->increment(...), 4),
    async(fn() => var_dump($example->get())),
]);
var_dump($example->get());
// int(9)
// int(9)


class Example {
    private int $value = 0;
    public function increment(int $count): void {
        $current = $this->value;
        delay(0);
        $this->value = $current + $count;
    }
    public function get(): int {
        return $this->value;
    }
}
class ExampleSynchronized {
    private Example $example;
    private ReadWriteLock $lock;
    public function __construct() {
        $this->example = new Example();
        $this->lock = new LocalReadWriteLock();
    }
    public function increment(int $count): void {
        synchronized(
            $this->lock->writeLock(),
            $this->example->increment(...), [$count],
        );
    }
    public function get(): int {
        return synchronized(
            $this->lock->readLock(),
            $this->example->get(...),
        );
    }
}