Page 1 of 1

Synchronizing multithreaded code?

Posted: Fri Dec 23, 2005 10:54 pm
by otseng
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?

Posted: Fri Dec 23, 2005 10:57 pm
by Jenk
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 :)

Posted: Fri Dec 23, 2005 11:02 pm
by josh
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

Posted: Fri Dec 23, 2005 11:10 pm
by otseng
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).

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.

Posted: Fri Dec 23, 2005 11:13 pm
by Jenk
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.

Posted: Fri Dec 23, 2005 11:31 pm
by otseng
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?

Posted: Fri Dec 23, 2005 11:37 pm
by josh
serialize your variable/object and write it to disk, you need to also implement file locking

Posted: Fri Dec 23, 2005 11:53 pm
by Jenk
otseng 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?
Even in Java static does not mean shared :P

Posted: Sat Dec 24, 2005 1:33 pm
by BDKR
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.
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).

I prototyped a load balancer in PHP. It used shared memory and access to that shared memory was controlled by semaphores.