downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

sem_get> <msg_stat_queue
[edit] Last updated: Fri, 25 May 2012

view this page in

sem_acquire

(PHP 4, PHP 5)

sem_acquireZugriff auf Semaphor anfordern

Beschreibung

bool sem_acquire ( resource $sem_identifier )

sem_acquire() blockiert (falls nötig) bis ein Zugriff auf den Semaphor möglich ist. Ein Prozess der Zugriff auf einen Semaphor anfordert auf den er bereits Zugriff hat wird für immer blockieren wenn dieser erneute Zugriff die maximalen Zugriffe auf diesen Semaphor überschreiten würde. sem_identifier ist eine von sem_get() gelieferte Semaphor-Resource.

Nach Abarbeitung eines requests werden alle noch vom Prozess gehaltenen Semaphore automatisch freigegeben und entsprechende Warnungen erzeugt.

Parameter-Liste

sem_identifier

sem_identifier ist ein von sem_get() geliefertes Resourcehandle

Rückgabewerte

Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.

Siehe auch



sem_get> <msg_stat_queue
[edit] Last updated: Fri, 25 May 2012
 
add a note add a note User Contributed Notes sem_acquire
Pinky 13-Feb-2012 08:29
sem_acquire() is blocking, meaning that subsequent calls with the same semaphore will block indefinitely until the semaphore is released. This ensures serialization, but it is not very practical if all you want to do is check if you should proceed or not. Unfortunately, PHP does not yet support any method of querying the state of a semaphore in a non-blocking manner.

It may seem possible to put together such a mechanism by hand, using shared memory (shm_ functions). However, be warned that it is not trivial and ultimately non-productive. You cannot, for example, simply pick a shared mem var, store the semaphore key and query it. Such an operation would be non-transactional and non-atomic ie. it is possible for two or more parallel processes to manage to read "not locked" from the shared mem var before one of them manages to mark it "locked". You would have to use a (blocking) semaphore to serialize access to the shared mem var, thus recreating the very problem you are trying to solve.

In other words, if non-blocking queries are crucial to you, you need to either request that this issue be solved by the PHP designers, or pick another mechanism to do your locking, one that already has this feature.
arjuna w 28-Jan-2012 01:14
In my tests sem_acquire() is 150 times faster than flock()
gladd at trash dot eris dot qinetiq dot com 13-Apr-2004 02:02
Just to clarify what is meant by "process" above:

On the Apache webserver, many PHP requests will be executed within the same process space because it is multithreaded. However, any semaphores got and acquired by a script and not released and removed will still be automatically cleaned up by the PHP interpreter each time the script terminates.

Remove any trash before emailing!

 
show source | credits | sitemap | contact | advertising | mirror sites