note 87198 deleted from function.flock by thiago
| From: | thiago@php.net | Date: | Mon, 24 Nov 2008 11:32:00 +0000 |
| Subject: | note 87198 deleted from function.flock by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146860@lists.php.net to get a copy of this message | ||
Note Submitter: Francesco
----
Last example is wrong. Regarding to * in code :
if flock is not obtained but was acquired in exclusive mode, this flock will hang waiting for the
lock to be released. Using LOCK_NB would enter that if block.
Correct me if i'm wrong.
<?php// waiting until file will be locked for writing (1000 milliseconds as timeout)
if ($fp = fopen($fileName, 'a')) {
$startTime = microtime();
do {
$canWrite = flock($fp, LOCK_EX);
// If lock not obtained sleep for 0 - 100 milliseconds, to avoid collision and CPU load *
if(!$canWrite) usleep(round(rand(0, 100)*1000));
} while ((!$canWrite)and((microtime()-$startTime) < 1000));
//file was locked so now we can store information
if ($canWrite) {
fwrite($fp, $dataToSave);
}
fclose($fp);
}?>