Re: [APC-DEV] atomaticity/thread safeness of apc shared memory functions+mutexes

From: Date: Thu, 25 Dec 2008 13:41:59 +0000
Subject: Re: [APC-DEV] atomaticity/thread safeness of apc shared memory functions+mutexes
References: 1 2 3 4 5  Groups: php.apc.dev 
Request: Send a blank email to php-apc-dev+get-178@lists.php.net to get a copy of this message
Exception e wrote: > Rasmus Lerdorf schreef: >> Exception e wrote: >>> Rasmus Lerdorf schreef: >>> >>>> Yes, they are atomic and they are locked appropriately. You don't need >>>> any sort of mutex. This is shared memory. Generally we don't suggest >>>> running PHP in any sort of threaded environment though. >>> To respond to your last remark: php processes can be exectuted in >>> parallel by Apache—at least this is my understanding. So if I need to >>> work with multiple shared memory entries per request i still need >>> mutexes in order to lock concurrent access to these pieces. >> >> That's not multi-threading, that is multi-processing, and of course APC >> handles that correctly and locks appropriately. Multi-threading is when >> you have multiple threads of execution within the same process. >> > > > Yes, but do you mean that all shared memory entries are blocked for the > duration of a process? Let's give me an example > > <?php > > $val1 = apc_fetch('val1'); > > // (#1) different process does: apc_store('val1', 34); > > $val2 = apc_fetch('val2'); > > echo $val1 + $val2; > > ?> > > > From what I understand is that when the same code runs in a different > process, the shared memory segment 'val1' can be changed on (#1) by that > other process. So in fact I need two mutexes. > > But if all shared memory is locked for each process, than such a > situation cannot happen. Did you really mean that? I hope you could > clarify this. apc_fetch() grabs a copy of the value in shared memory. What you do with it locally in the process after the fetch is irrelevant. It is just a copy. You can then overwrite the value in shared memory with a new version with an apc_store() call. Locking shared memory the way you suggest would be a really bad idea. It would be a performance nightmare. You need to design your code in a way that does not require that. APC 3.1 has functions that let you atomically increment and decrement a value in shared memory. See the apc_inc() and apc_dec() functions. That is the only way that makes sense to manipulate shared memory entries in a way resembling what you are suggesting. -Rasmus

« previous php.apc.dev (#178) next »