note 64096 added to function.flock
| From: | christian dot wessels at web dot de | Date: | Fri, 07 Apr 2006 09:41:49 +0000 |
| Subject: | note 64096 added to function.flock | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-107506@lists.php.net to get a copy of this message | ||
dranger at export dash japan dot com is right, i once had the problem of a race condition in a
database action because the script was very cpu-intensive so i looked around for some kind of mutex.
I finally came up to this which uses no files (and no flock) and survived all of my tests (on
linux):
<?php
function generate_shmkey ($string)
{
// Private: output unique integer based on $string
return abs(crc32($string));
}
function vmutex_lock ($lockId, $maxAcquire = 1)
{
// Public: take cover of semaphores and lock when possible
global $shm_handle;
$shm_key = generate_shmkey($lockId);
if ($shm_handle = sem_get($shm_key, $maxAcquire))
{
if (sem_acquire($shm_handle))
{
return true;
}
}
return false;
}
function vmutex_unlock ($lockId)
{
// Public: remove lock
global $shm_handle;
return sem_release($shm_handle);
}
?>
----
Server IP: 217.13.201.10
Probable Submitter: 87.123.214.9
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.flock.php
Edit -- http://master.php.net/note/edit/64096
Del: integrated -- http://master.php.net/note/delete/64096/integrated
Del: useless -- http://master.php.net/note/delete/64096/useless
Del: bad code -- http://master.php.net/note/delete/64096/bad+code
Del: spam -- http://master.php.net/note/delete/64096/spam
Del: non-english -- http://master.php.net/note/delete/64096/non-english
Del: in docs -- http://master.php.net/note/delete/64096/in+docs
Del: other reasons-- http://master.php.net/note/delete/64096
Reject -- http://master.php.net/note/reject/64096
Search -- http://master.php.net/manage/user-notes.php