Lock¶
A lock is a mechanism used to control access to a shared resource so that only one process (or script) can use it at a time.
There are different kind of lock:
file locks, when using flock() function
database locks, via transactions or explicit locks in the database
semaphore locks, based on sem extension.
<?php
$sem = sem_get(123456);
sem_acquire($sem);
// critical section
sem_release($sem);
?>
See also Data locking strategies in PHP apps. Practical approach and Locking in PHP to serialize execution of critical sections.
Related : Atomic Operation, File Mode, file_put_contents(), Race Condition
Related packages : php-lock/lock