确保多个进程同时写入同一个文件成功代码
function wirte($path,$mode,$data){
$fp=fopen($path,$mode);
$retries=0;
$max_retries=100;
do{
if($retries>0){
usleep(rand(1,10000));
}
$retries+=1;
}while(!flock($fp,LOCK_EX)and $retries <= $max_retries);
if($retries==$max_retries){
return false;
}
fwrite($fp,"$data");
flock($fp,LOCK_UN);
fclose($fp);
return true;
}
echo wirte('1.txt','w+',"wwwwwwwwww");