PHP code example of texthtml / php-lock

1. Go to this page and download the library: Download texthtml/php-lock library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

texthtml / php-lock example snippets


use TH\Lock\FileLock;

$lock = new FileLock('/path/to/file');

$lock->acquire();

// other processes that try to acquire a lock on the file will fail

// edit /path/to/file

$lock->release();

// other processes can now acquire a lock on the file

use TH\Lock\FileLock;

$lock = new FileLock('/path/to/file', FileLock::SHARED);

$lock->acquire();

// other processes that try to acquire an exclusive lock on the file will fail,
// processes that try to acquire an shared lock on the file will succeed

// read /path/to/file

$lock->release();

// other processes can now acquire an exclusive lock on the file if no other shared lock remains.

use TH\Lock\FileLock;

function batch() {
    $lock = new FileLock('/some/file');
    $lock->acquire();

    // lot of editing on file
}

batch();

// the lock will be released here even if $lock->release() is not called in batch()

$lock = $factory->create('protected resource', 'process 1');

$lock->acquire();

// process 1 does stuff

$lock = $factory->create('protected resource', 'process 2');

$lock->acquire();

// process 2 does stuff

use TH\Lock\FileFactory;

$factory = new FileFactory('/path/to/lock_dir/');
$lock = $factory->create('resource identifier');

use TH\Lock\LockSet;

$superLock = new LockSet([$lock1, $lock2, $lock3]);
// You can make a set with any types of locks (eg: FileLock, RedisSimpleLock or another nested LockSet)

$superLock->acquire();

// all locks will be released when $superLock is destroyed or when `$superLock->release()` is called
bash
composer