note 74493 deleted from function.flock by nlopess
| From: | nlopess@php.net | Date: | Sun, 15 Apr 2007 11:18:21 +0000 |
| Subject: | note 74493 deleted from function.flock by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125414@lists.php.net to get a copy of this message | ||
Note Submitter: marco at hotelsandgo dot com
----
Alternative to flock using spin-lock (completely portable, in theory works on all filesystems and
all OS):
function _acquireFileMutex($s) {
while(file_exists($s)) sleep(1);
$mutex=fopen($s, 'w');
global $mutex;
}
function _releaseFileMutex($s) {
fclose($mutex);
unlink($s);
}
Notes:
a) $s is a lock-file
b) sleep(1) is only for optimization (reduces spin-lock by suspending directly on the scheduler); in
my behavior 1 second is appropriate.
You can remove it (replace with a ;) or replace with a usleep, time_nanosleep, or similar...
I stress tested it, is so simple :)
Ideas: This is completely portable, if you want something more professional, busy wait can be
removed using pipes... (php has function to create pipes, however i'm not sure that are
portables)