I am mostly familiar with Java, C and C ++, in which there are ways to control that only one thread is accessing a resource at any given time. Now I am looking for something similar, but in PHP 5.x.
To state my problem with one example:
I have an ASCII file that stores only the number, the value of the page load counter. When you deploy the application, the file will simply contain 0. For each access, the value will increase by one. The goal is to track page loads.
The problem occurs when many users access the page containing the counter at the same time. When thread A reads the current value, let's say it's 11, another thread that we call B reads the value, another 11. Then the first thread A increases the read value and writes 12 to the file and closes it. Then the second thread B increases the read value, which is 11, gets 12 and writes it to a file. The value 12 is stored in the file when it really should be 13.
In another programming language, I would solve this with a mutex. I understand that the modules have mutexes, shared memory, and other functionality. But I would like to see a solution that works on "most servers" there. Independent platform. Installed on the cheapest websites. Is there a good solution to this problem? And if not, how would you take it if using a database is not an option ?
synchronization file php mutex
user14070
source share