Synchronizing multithreaded code?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
otseng
Forum Newbie
Posts: 3
Joined: Tue Apr 22, 2003 1:02 pm
Contact:

Synchronizing multithreaded code?

Post 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?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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 :)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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
otseng
Forum Newbie
Posts: 3
Joined: Tue Apr 22, 2003 1:02 pm
Contact:

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
otseng
Forum Newbie
Posts: 3
Joined: Tue Apr 22, 2003 1:02 pm
Contact:

Post 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?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

serialize your variable/object and write it to disk, you need to also implement file locking
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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.
Post Reply