Semaphore
继承: RefCounted
< Object
A synchronization mechanism used to control access to a shared resource by Thread
s.
描述
A synchronization semaphore that can be used to synchronize multiple Thread
s. Initialized to zero on creation. For a binary version, see Mutex
.
Warning: Semaphores must be used carefully to avoid deadlocks.
Warning: To guarantee that the operating system is able to perform proper cleanup (no crashes, no deadlocks), these conditions must be met:
-
When a Semaphore's reference count reaches zero and it is therefore destroyed, no threads must be waiting on it.
-
When a
Thread
's reference count reaches zero and it is therefore destroyed, it must not be waiting on any semaphore.
方法
方法说明
void
post ( count: int
= 1 )
Lowers the Semaphore, allowing one thread in, or more if count
is specified.
bool
try_wait ( )
Like wait
, but won't block, so if the value is zero, fails immediately and returns false
. If non-zero, it returns true
to report success.
void
wait ( )
Waits for the Semaphore, if its value is zero, blocks until non-zero.
本方法通常需要用户覆盖才能生效。
本方法无副作用,不会修改该实例的任何成员变量。
本方法除了能接受在此处描述的参数外,还能够继续接受任意数量的参数。
本方法用于构造某个类型。
调用本方法无需实例,可直接使用类名进行调用。
本方法描述的是使用本类型作为左操作数的有效运算符。
这个值是由下列位标志构成位掩码的整数。
无返回值。