我使用php信号量真的是线程安全的吗?
我们有一个php脚本,由dhcp服务器在某些事件上执行。 如果有许多用户,这些事件可能会经常发生,并且由于脚本具有某些依赖关系,因此只能逐个执行。为了实现这一点,我们使用一个信号量,这就是它的实现方式我使用php信号量真的是线程安全的吗?,php,concurrency,semaphore,Php,Concurrency,Semaphore,我们有一个php脚本,由dhcp服务器在某些事件上执行。 如果有许多用户,这些事件可能会经常发生,并且由于脚本具有某些依赖关系,因此只能逐个执行。为了实现这一点,我们使用一个信号量,这就是它的实现方式 public function getGlobalLock() { $this->semaphore = sem_get($this->sem_key, 1, $this->sem_perm, 1); if(!$this->semaphore) {
public function getGlobalLock() {
$this->semaphore = sem_get($this->sem_key, 1, $this->sem_perm, 1);
if(!$this->semaphore) {
// throw exception because we always should be able to do this!
// this is a serious error
throw new Exception("Error. Locking was not possible, unable to retrieve semaphore key. PID " . $this->pid . ", purpose for lock is: " . $this->purpose);
}
// blocks while waiting for semaphore
if(!sem_acquire($this->semaphore)) {
// throw exception because we always should be able to do this!
// serious error
throw new Exception("Error. Locking was not possible, unable to acquire semaphore. PID " . $this->pid . ", purpose for lock is: " . $this->purpose);
}
}
现在的问题是,我们对脚本的行为有一些问题,一个想法是信号量的工作方式与我们期望的不一样。
有人能证明这应该保证顺序吗?这很容易测试。写一个测试,在那里你拿着信号灯,睡几秒钟。与此同时,另一个电话试图得到它。事实上,我们做了测试,我们没有发现任何失败,但我们认为这可能只是因为我们运气好,要验证这一点需要花费大量的时间和精力。因此,如果有人已经将想法投入其中,这可能会有所帮助。