Is there a way to synchronize code to prevent multiple simultaneous access to a function?
I have some static functions that manipulate a static array. And I want to make sure that only one person at a time is modifying the array. Otherwise, the array could get into an inconsistent state. Is there a way to do this?
Synchronizing multithreaded code?
Moderator: General Moderators
PHP doesn't do multi-threading.
Besides, if the array is only in memory, only one user will have access to it.
If the array is structured from say a flat file, then you'll need to lock the file everytime it is used, and unlock it when the function has finished.
Please post some code to clarify so we can assist further
Besides, if the array is only in memory, only one user will have access to it.
If the array is structured from say a flat file, then you'll need to lock the file everytime it is used, and unlock it when the function has finished.
Please post some code to clarify so we can assist further
if somehow you are using PHP to alter something that persists in memory only and there is a chance of two concurrent processes modifying it simultaneously (no idea how this would happen), have a variable that holds the name of the process that currently has the "lock" and have your application wait till the other process releases the lock before it obtains the lock
but like jenk said this is pretty much impossible (array in memory edited by two processes) as a variable in PHP's global scope only extends to the current process
but like jenk said this is pretty much impossible (array in memory edited by two processes) as a variable in PHP's global scope only extends to the current process
The function would be accessing a static array (in memory).but like jenk said this is pretty much impossible (array in memory edited by two processes) as a variable in PHP's global scope only extends to the current process
My concern is two (or more) sessions hitting the exact same code at the same time. What I want to do is create an object pool in memory. There will be two functions: acquire() and release(). The acquire() will pop an object from the pool. The release() will push it back into the pool.
If the acquire is not synchronized, then it could be possible that two users could get the same object from the pool. I guess it looks like I'll have to write my own semaphore code to try to avoid this situation.
That does not and cannot happen in PHP.
The array's are held in memory. Memory is allocated on a per-process basis. Each user on your system will have their own process. The array is stored in memory, thus each user will have their own array, static or not static, users cannot access another users array.
please read what we have posted.
The array's are held in memory. Memory is allocated on a per-process basis. Each user on your system will have their own process. The array is stored in memory, thus each user will have their own array, static or not static, users cannot access another users array.
please read what we have posted.
Even in Java static does not mean sharedotseng wrote:Ah, coming from a Java background, I assumed that static would have the same meaning. I guess it doesn't.
I guess my next question would be is there a way to make a variable be persisted in memory and accessible by all? Or a followup question, is there a way to write a memory object pool in php?
If a person chose to place some data in shared memory then the risk of it being accessed can be a problem. There are instances where shared memory is a pretty good option. Apache uses shared memory and all it's processes are somewhat divorced from one another (after forking of course).Jenk wrote:That does not and cannot happen in PHP.
The array's are held in memory. Memory is allocated on a per-process basis. Each user on your system will have their own process. The array is stored in memory, thus each user will have their own array, static or not static, users cannot access another users array.
please read what we have posted.
I prototyped a load balancer in PHP. It used shared memory and access to that shared memory was controlled by semaphores.