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 its function(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, return False.

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. See unlock() for explanation of when function(argument) is executed in that case.

mutex.unlock()

Unlock the mutex if queue is empty, otherwise execute the first element in the queue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:14 次

字数:2136

最后编辑:6 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文