note 74493 added to function.flock
| From: | marco at hotelsandgo dot com | Date: | Fri, 13 Apr 2007 20:39:42 +0000 |
| Subject: | note 74493 added to function.flock | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-125368@lists.php.net to get a copy of this message | ||
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)
----
Server IP: 213.92.11.19
Probable Submitter: 192.92.126.136 (proxied: HTTP/1.0 infocache[C05C7E88] (Traffic-Server/3.0.3
[uNcM]))
----
Manual Page -- http://www.php.net/manual/en/function.flock.php
Edit -- https://master.php.net/note/edit/74493
Del: integrated -- https://master.php.net/note/delete/74493/integrated
Del: useless -- https://master.php.net/note/delete/74493/useless
Del: bad code -- https://master.php.net/note/delete/74493/bad+code
Del: spam -- https://master.php.net/note/delete/74493/spam
Del: non-english -- https://master.php.net/note/delete/74493/non-english
Del: in docs -- https://master.php.net/note/delete/74493/in+docs
Del: other reasons-- https://master.php.net/note/delete/74493
Reject -- https://master.php.net/note/reject/74493
Search -- https://master.php.net/manage/user-notes.php