1. Distinguish between read lock and write lock.
If write locking is used every time, multiple processes will have to queue up to read a file, which is definitely not efficient.
2. Distinguish between blocking and non-blocking modes.
Generally speaking, if one process is writing a file, the other process should be blocked. However, many times, we can do something else first,
and then determine whether anyone else is there. Write the file, and if it doesn't exist, add the data, which is more efficient.
3. Fixed the bug of locking files on Linux, especially the bug on the gfs file system.
The code is as follows:
Copy code The code is as follows:
class File_Lock
{
private $name;
private $handle;
private $mode;
function __construct($filename, $mode = 'a+b')
{
global $php_errormsg ;
$this->name = $filename;
$path = dirname($this->name);
if ($path == '.' || !is_dir($path) ) {
global $config_file_lock_path;
$this->name = str_replace(array("/", "\"), array("_", "_"), $this->name) ;
if ($config_file_lock_path == null) {
$this->name = dirname(__FILE__) . "/lock/" . $this->name;
} else {
$this->name = $config_file_lock_path . "/" . $this->name;
}
}
$this->mode = $mode;
$this-> handle = @fopen($this->name, $mode);
if ($this->handle == false) {
throw new Exception($php_errormsg);
}
}
public function close()
{
if ($this->handle !== null ) {
@fclose($this->handle);
$this- >handle = null;
}
}
public function __destruct()
{
$this->close();
}
public function lock($ lockType, $nonBlockingLock = false)
{
if ($nonBlockingLock) {
return flock($this->handle, $lockType | LOCK_NB);
} else {
return flock ($this->handle, $lockType);
}
}
public function readLock()
{
return $this->lock(LOCK_SH);
}
public function writeLock($wait = 0.1)
{
$startTime = microtime(true);
$canWrite = false;
do {
$canWrite = flock($this ->handle, LOCK_EX);
if(!$canWrite) {
usleep(rand(10, 1000));
}
} while ((!$canWrite) && ((microtime (true) - $startTime) < $wait));
}
/**
* if you want't to log the number under multi-thread system,
* please open the lock, use a+ mod. then fopen the file will not
* destroy the data.
*
* this function increment a delt value , and save to the file.
*
* @param int $delt
* @return int
*/
public function increment($delt = 1)
{
$n = $this->get();
$n += $delt;
$this->set($n);
return $n;
}
public function get()
{
fseek($this->handle, 0);
return (int)fgets($this->handle);
}
public function set( $value)
{
ftruncate($this->handle, 0);
return fwrite($this->handle, (string)$value);
}
public function unlock()
{
if ($this->handle !== null ) {
return flock($this->handle, LOCK_UN);
} else {
return true;
}
}
}
?>
Test code:
Copy code The code is as follows:
/**
* Test write lock
* Open thread 1
*/
require("file_lock.php");
$lock = new File_Lock (dirname(dirname(__FILE__)) . "/FileLock.lock");
/**The locking speed of a single thread is 30,000 times per second. **/
/**Two threads write, 20,000 data, it takes about 7 seconds*/
/**It takes about 3.9 seconds to write 10,000 data with one thread. Actually, two files are written at the same time, which is faster.*/
/**Without locking, it takes about 2.8 seconds for a process to write. There is a price for locking.*/
/**Without locking, the distribution of the two processes is not very even, and most of them conflict.*/
$lock->writeLock();
$lock->increment();
$ lock->unlock();
while ($lock->get() < 2) {
usleep(1000);
}
sleep(1);
echo "begin to running n";
$t1 = microtime(true);
for ($i = 0; $i < 10000; $i++)
{
$lock->writeLock ();
$lock->increment(1);
$lock->unlock();
}
$t2 = microtime(true) - $t1;
echo $t2;
?>
I added an increment function, which can achieve simple thread synchronization and allow two processes to execute a certain code at the same time. Of course, this has a certain error
The error here is 0.001s.
Simply use this class in the previous memcache message queue to implement a thread-safe message queue.
http://www.bkjia.com/PHPjc/320820.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320820.htmlTechArticle1. Distinguish read locks and write locks. If write locking is used every time, multiple processes will have to queue up to read a file, which is definitely not efficient. 2. Distinguish between blocking and non...