f3ath/mysqlmutex

A simple mutex making use of MySQL GET_LOCK() function

0.0.1 2014-04-16 22:42 UTC

README

A simple mutex making use of MySQL GET_LOCK() function

Install

Via composer: $ composer require "f3ath/mysqlmutex"

Example

// example.php
<?php
require_once __DIR__.'/vendor/autoload.php';
$pdo = new PDO('mysql:dbname=test', 'test', 'test', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
$lock = new F3\MySQLMutex\Lock($pdo);
$pid = getmypid();
for ($i = 0; $i < 10; $i++) {
    echo "$pid: trying...\n";
    if ($lock->acquire('zzz', 1)) {
        echo "$pid: LOCKED!\n";
        sleep(mt_rand(1, 3));
        $lock->release('zzz');
        echo "$pid: RELEASED\n";
    }
}

Run 3 instances and see what happens

$ php example.php & php example.php & php example.php &