update page now

Voting

: min(five, one)?
(Example: nine)

The Note You're Voting On

forzi at mail333 dot com
10 years ago
Simple Helper for lock files creation
<?php
class FileLocker {
 protected static $loc_files = array();
 public static function lockFile($file_name, $wait = false) {
 $loc_file = fopen($file_name, 'c');
 if ( !$loc_file ) {
 throw new \Exception('Can\'t create lock file!');
 }
 if ( $wait ) {
 $lock = flock($loc_file, LOCK_EX);
 } else {
 $lock = flock($loc_file, LOCK_EX | LOCK_NB);
 }
 if ( $lock ) {
 self::$loc_files[$file_name] = $loc_file;
 fprintf($loc_file, "%s\n", getmypid());
 return $loc_file;
 } else if ( $wait ) {
 throw new \Exception('Can\'t lock file!');
 } else {
 return false;
 }
 }
 public static function unlockFile($file_name) {
 fclose(self::$loc_files[$file_name]);
 @unlink($file_name);
 unset(self::$loc_files[$file_name]);
 }
} 
if ( !FileLocker::lockFile('/tmp/1.lock') ) {
 echo "Can't lock file\n";
 die();
}
sleep(10);
FileLocker::unlockFile('/tmp/1.lock');
echo "All Ok\n";

<< Back to user notes page

AltStyle によって変換されたページ (->オリジナル) /