PHP can also be multithreaded, so the problem comes, and that is the problem of synchronization. Changping Computer Training knows that PHP itself does not support multithreading, so there will be no synchronize method like Java. So what should we do?
1. Try not to access the same resource. In order to avoid conflict. But it can run like a database at the same time. Because the database supports concurrent operations. So don't write data to the same file in multithreaded PHP. If you must write, use other methods to synchronize. For example, call flock to lock the file. Or create a temporary file and wait for the disappearance of this file in another thread while(file_exits('xxx')); This means that when this temporary file exists, it means that the thread is actually running. If this file is lost, it means that other threads have released it.
2. Try not to read data from this socket obtained by runThread after executing fputs. Because to realize multithreading, you must use non-blocking mode. In other words, when a function like fgets is used, it will return immediately. . So reading and writing data will cause problems. If blocking mode is used, the program is not multithreaded. Before performing the following procedure, he must wait for the above return. So if you need to exchange data, you can finally use external files or data to complete it. If you really want it, use socket_set_nonblock($fp) to realize it.
Having said that, is there any practical significance in it? When do you need this method?
The answer is yes. As we all know. In an application that constantly reads network resources, the network speed is the bottleneck. If this form is adopted, different pages can be read by multiple threads at the same time.