0

I'm using mysql get_lock to ensure mutual exclusion between multiple instances of php scripts that I am running for the same lines of code. I need to ensure that two simultaneous executions of the is_free_lock will not yield 0, in which case I will end up locking by both instances of the script and then execute that code twice. Please help.

if($lock->isLockFree()) {
    // lock

    // release lock
} else {
    // trigger already locked error
}
YD8877
  • 10,401
  • 20
  • 64
  • 92

1 Answers1

0

Don't use IS_FREE_LOCK(str). You just should use GET_LOCK(str,timeout) and check if it returns 1 (success, lock gained) or 0 (fail, lock already gained). So your code should look like this:

// try to lock
if($lock->getLock()) {
    // locked

    // release lock
    $lock->releaseLock();
} else {
    // trigger already locked error
}

Where getLock() should use GET_LOCK(str,timeout)

BTW if you use php you may want to try my ninja-mutex library for locking.

Kamil Dziedzic
  • 4,721
  • 2
  • 31
  • 47