This article mainly introduces PHP based on file lock to solve multiple processes at the same time read and write a file, combined with examples of PHP use flock for file read and write lock operation usage, the need for friends can refer to the next
Specific as follows:
First of all, PHP is to support the process and does not support multithreading (this first clear), if it is for the file operation, in fact, you just need to add a lock to the file can be resolved, do not need other operations, PHP flock has helped you get it done.
With flock before writing the file lock, and so on after the completion of the lock, so that the implementation of multi-threaded and read and write a file to avoid conflict. It's probably the next process.
/**flock (File,lock,block) *file required, which specifies that an open file to be locked or released *lock required. Specifies which type of lock to use. *block is optional. If set to 1 or true, the other process is blocked when the lock is made. *lock*lock_sh to obtain a shared lock (read program) *LOCK_EX to obtain an exclusive lock (written program) *lock_un to release the lock (whether shared or exclusive) *LOCK_NB if you do not want flock () to Clog/*if (flock ($ FILE,LOCK_EX) {fwrite ($file, ' write more words '); Flock ($file, lock_un);} else{//Handling Error Logic}fclose ($file);)
Related recommendations:
PHP uses file locks to resolve high concurrency steps
Talking about PHP file lock
php file lock resolves high concurrency