PHP code example of f3ath / flock

1. Go to this page and download the library: Download f3ath/flock 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/ */

    

f3ath / flock example snippets


$file = '/tmp/my_lock.pid';
$lock = new F3\Flock\Lock($file);

// Non-blocking case. Acquire lock if it's free, otherwse exit immediately
if ($lock->acquire()) {
    // only one instance can reach here
    ...
    // do some job
    ...
    $lock->release();
} else {
    die('Another process is running')
}


// Waiting case. Acquire lock if it's free, otherwse block until it's free and then acquire
if ($lock->acquire(F3\Flock\Lock::BLOCKING)) {
    // only one instance can reach here
    ...
    // do some job
    ...
    $lock->release();
} else {
    // We sould not get to this point in this case
}