原贴:http://blog.chinaunix.net/u/27731/showart_462502.html
独占的方式读写文件,经jmeter1000个线程并发访问测试没问题 |
![](http://blog.chinaunix.net/templates/default/images/right_line.gif) |
独占的方式读写文件,经jmeter1000个线程并发访问测试没问题
<?php $filename = 't.txt'; print readToWrite($filename);
function readToWrite($filename){ if($fp = @fopen($filename, 'r+b')) { // 打开文件流 $content = 0; flock($fp, LOCK_EX); // 进行排它型锁定 $content = file_get_contents($filename); $content = $content + 1; fwrite($fp, $content); flock($fp, LOCK_UN); // 释放锁定 fclose($fp); return $content; } else { throw new Exception('发生问题了'); } }
?>
| |