Last active
April 30, 2018 22:16
-
-
Save naosim/61a908ad2e7393cae69f972ddc356d8d to your computer and use it in GitHub Desktop.
phpでlockする
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function lock($lockFileName, $fuc) { | |
if(!file_exists($lockFileName)) { | |
file_put_contents($lockFileName, ''); | |
} | |
$lock_fp = fopen($lockFileName ,"w"); | |
flock($lock_fp, LOCK_EX); | |
try { | |
return $fuc(); | |
} finally { | |
fclose($lock_fp); | |
} | |
} | |
// USAGE | |
// $count = 0; | |
// while(true) { | |
// lock('locktest', function() use (&$count) { | |
// print($count++ . "\n"); | |
// sleep(1); | |
// }); | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment