8.9. mutex — Mutual exclusion support - Python 2.7.18 documentation 编辑
Deprecated since version 2.6: The mutex
module has been removed in Python 3.
The mutex
module defines a class that allows mutual-exclusion via acquiring and releasing locks. It does not require (or imply) threading
or multi-tasking, though it could be useful for those purposes.
The mutex
module defines the following class:
- class
mutex.
mutex
Create a new (unlocked) mutex.
A mutex has two pieces of state — a “locked” bit and a queue. When the mutex is not locked, the queue is empty. Otherwise, the queue contains zero or more
(function, argument)
pairs representing functions (or methods) waiting to acquire the lock. When the mutex is unlocked while the queue is not empty, the first queue entry is removed and itsfunction(argument)
pair called, implying it now has the lock.Of course, no multi-threading is implied – hence the funny interface for
lock()
, where a function is called once the lock is acquired.
8.9.1. Mutex Objects
mutex
objects have following methods:
mutex.
test
()Check whether the mutex is locked.
mutex.
testandset
()“Atomic” test-and-set, grab the lock if it is not set, and return
True
, otherwise, returnFalse
.
mutex.
lock
(function, argument)Execute
function(argument)
, unless the mutex is locked. In the case it is locked, place the function and argument on the queue. Seeunlock()
for explanation of whenfunction(argument)
is executed in that case.
mutex.
unlock
()Unlock the mutex if queue is empty, otherwise execute the first element in the queue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论