略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: sem_release

2024-12-22

sem_release

(PHP 4, PHP 5, PHP 7, PHP 8)

sem_releaseRelease a semaphore

说明

sem_release(SysvSemaphore $semaphore): bool

sem_release() releases the semaphore if it is currently acquired by the calling process, otherwise a warning is generated.

After releasing the semaphore, sem_acquire() may be called to re-acquire it.

参数

semaphore

A Semaphore as returned by sem_get().

返回值

成功时返回 true, 或者在失败时返回 false

更新日志

版本 说明
8.0.0 semaphore expects a SysvSemaphore instance now; previously, a resource was expected.

参见

add a noteadd a note

User Contributed Notes 2 notes

up
2
danno at circumsolutions dot com
21 years ago
If you want to implement this sort of semaphore access (which is usually what is needed if doing ipc) one thing you can do is create shared memory which is simply an int. Use this int between processes as the value of the semaphore.  You can write wrapper functions which use this to allow you to do the desired readers-writers stuff or just use the shared memory plain.  I wouldn't be too worried about race conditions in this case as the shared memory is only an int.  If you are worried about race conditions, the wrapper functions for doing the semaphore stuff could actually use a semaphore to elimate any race conditions.  Also, an added bonus of doing it this way is you can check the value of the int.
up
2
apua at communiplex dot com
22 years ago
The fact that sem_release only resease the semaphore if it is currently acquired by the calling process is, in fact, a misfeature since sometimes (eg in a readers-writers implementation) it is desirable to release a semaphore acquired by another process. In C you can do that.
I'm telling this because I was trying to write a chat app in php3 made of two programs: a listener and a speaker. The speaker releases a semaphore to inform that there is a new message in a shared memory var. And listeners wait in that semaphore before outputting the message.

官方地址:https://www.php.net/manual/en/function.sem-release.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3