1 function writeData($path, $mode, $data){ 2 $fp = fopen($path, $mode); 3 $retries = 0; 4 $max_retries = 100; 5 do { 6 if ($retries > 0) { 7 usleep(rand(1, 10000)); 8 } 9 $retries += 1; 10 }while (!flock($fp, LOCK_EX) and $retries <= $max_retries); 11 12 if ($retries == $max_retries) { 13 return false; 14 } 15 16 fwrite($fp, "$data\n"); 17 flock($fp, LOCK_UN); 18 fclose($fp); 19 return true; 20 }
原文:http://www.cnblogs.com/gengyi/p/6372030.html