note 62568 deleted from function.flock by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 00:38:49 +0000 |
| Subject: | note 62568 deleted from function.flock by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155403@lists.php.net to get a copy of this message | ||
Note Submitter: Niels Jaeckel
----
hi dranger at export dash japan dot com,
you are right with your thoughts about process-switching. But I think the following kind of code
would work in race-conditions, because it is very improbably that the CPU switches after just ONE
statement:
<?php
// here my own lock-function WITHOUT flock()
// you can call it a mutex...
// lock ... compare to P(mutex)
function lock($filename) {
$filename .= '.lock';
while (true) {
if (! file_exists($filename)) {
if (! file_exists($filename)) {
if (! file_exists($filename)) {
return touch($filename);
}
}
}
}
}
// unlock ... compare to V(mutex)
function unlock($filename) {
unlink($filename . '.lock');
}
?>
If you want to solve the reader/writer problem (many readers, just one exclusive writer) you can add
some flock() - code instead of touch(...)
hth