PHP lock on concurrent operations

Source: Internet
Author: User
PHP locks concurrent php operations. we all know how to solve multithreading to read and write the same file. PHP does not have the concept of multithreading. Even so, we can still use an "imperfect" method to simulate multithreading. In short, it is queue processing. You can lock and unlock files. When a file is operated by a user, the file is locked, and other users can only wait, but it is not perfect, but it can also meet the locking requirements for PHP concurrent operations.

How php can read and write the same file with multiple threads

As we all know, PHP does not have the concept of multithreading. Even so, we can still use the "imperfect" method to simulate multithreading. In short, it is queue processing. You can lock and unlock files. When a file is operated by a user, the file is locked. other users can only wait, but it is not perfect, but it can also meet the requirements of some applications.

There is a need: When a file is generated, multiple users have the permission to generate the file to prevent errors in the generated results due to concurrency, and the generated process needs to be locked, only one user is allowed to perform operations within a period of time. a lock is required to lock the operation process. When the cache is used, the cache failure may cause most concurrent requests to penetrate to the database instantly. at this time, you also need to lock the operation in the same concurrency process.

In the above two cases, the solution is to lock the processing process, and implement the following through PHP:

The memory lock and file lock of the Eaccelerator are used. The principle is to determine whether the EAccelerator is installed in the system. If yes, the memory lock is used. If no Eaccelerator exists, the file lock is implemented. Multiple locks can be directly processed in parallel based on different imported keys, similar to the row-level locks of Innodb.

The specific classes are as follows:

 EAccelerator = function_exists ("eaccelerator_lock"); if (! $ This-> eAccelerator) {if (! Is_dir ($ path) {mkdir ($ path, 0777) ;}$ this-> path = $ path. ($ this-> _ mycrc32 ($ name) % $ this-> hashNum).'.txt ';} $ this-> name = $ name ;} /*** crc32 * crc32 encapsulation * @ param int $ string * @ return int */private function _ mycrc32 ($ string) {$ crc = abs (crc32 ($ string); if ($ crc & 0x80000000) {$ crc ^ = 0 xffffffff; $ crc + = 1 ;} return $ crc;}/*** lock * Enter description here... */public function lock () {// if the ea memory lock cannot be enabled, enable the file lock if (! $ This-> eAccelerator) {// you can configure the directory permission to write $ this-> fp = fopen ($ this-> path, 'W + '); if ($ this-> fp ==== false) {return false;} return flock ($ this-> fp, LOCK_EX );} else {return eaccelerator_lock ($ this-> name) ;}}/*** unlock * Enter description here... */public function unlock () {if (! $ This-> eAccelerator) {if ($ this-> fp! = False) {flock ($ this-> fp, LOCK_UN); clearstatcache () ;}// close fclose ($ this-> fp );} else {return eaccelerator_unlock ($ this-> name) ;}}}?>

? Use:

$ Lock = new CacheLock ('key _ name'); $ lock-> lock (); // logic here $ lock-> unlock (); // note that the write permission is required for the path of the file lock.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.