| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="rlock-objects.html" /> |
| 13 | <link rel="prev" href="module-threading.html" /> |
| 14 | <link rel="parent" href="module-threading.html" /> |
| 15 | <link rel="next" href="rlock-objects.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>7.5.1 Lock Objects </title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="7.5 threading " |
| 25 | href="module-threading.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="7.5 threading " |
| 28 | href="module-threading.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="7.5.2 RLock Objects" |
| 31 | href="rlock-objects.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="module-threading.html">7.5 threading </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-threading.html">7.5 threading </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="rlock-objects.html">7.5.2 RLock Objects</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION009510000000000000000"></A><A NAME="lock-objects"></A> |
| 56 | <BR> |
| 57 | 7.5.1 Lock Objects |
| 58 | </H2> |
| 59 | |
| 60 | <P> |
| 61 | A primitive lock is a synchronization primitive that is not owned |
| 62 | by a particular thread when locked. In Python, it is currently |
| 63 | the lowest level synchronization primitive available, implemented |
| 64 | directly by the <tt class="module"><a href="module-thread.html">thread</a></tt> extension module. |
| 65 | |
| 66 | <P> |
| 67 | A primitive lock is in one of two states, ``locked'' or ``unlocked''. |
| 68 | It is created in the unlocked state. It has two basic methods, |
| 69 | <tt class="method">acquire()</tt> and <tt class="method">release()</tt>. When the state is |
| 70 | unlocked, <tt class="method">acquire()</tt> changes the state to locked and returns |
| 71 | immediately. When the state is locked, <tt class="method">acquire()</tt> blocks |
| 72 | until a call to <tt class="method">release()</tt> in another thread changes it to |
| 73 | unlocked, then the <tt class="method">acquire()</tt> call resets it to locked and |
| 74 | returns. The <tt class="method">release()</tt> method should only be called in the |
| 75 | locked state; it changes the state to unlocked and returns |
| 76 | immediately. When more than one thread is blocked in |
| 77 | <tt class="method">acquire()</tt> waiting for the state to turn to unlocked, only one |
| 78 | thread proceeds when a <tt class="method">release()</tt> call resets the state to |
| 79 | unlocked; which one of the waiting threads proceeds is not defined, |
| 80 | and may vary across implementations. |
| 81 | |
| 82 | <P> |
| 83 | All methods are executed atomically. |
| 84 | |
| 85 | <P> |
| 86 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 87 | <td><nobr><b><tt id='l2h-2720' xml:id='l2h-2720' class="method">acquire</tt></b>(</nobr></td> |
| 88 | <td><var></var><big>[</big><var>blocking<code> = 1</code></var><big>]</big><var></var>)</td></tr></table></dt> |
| 89 | <dd> |
| 90 | Acquire a lock, blocking or non-blocking. |
| 91 | |
| 92 | <P> |
| 93 | When invoked without arguments, block until the lock is |
| 94 | unlocked, then set it to locked, and return true. |
| 95 | |
| 96 | <P> |
| 97 | When invoked with the <var>blocking</var> argument set to true, do the |
| 98 | same thing as when called without arguments, and return true. |
| 99 | |
| 100 | <P> |
| 101 | When invoked with the <var>blocking</var> argument set to false, do not |
| 102 | block. If a call without an argument would block, return false |
| 103 | immediately; otherwise, do the same thing as when called |
| 104 | without arguments, and return true. |
| 105 | </dl> |
| 106 | |
| 107 | <P> |
| 108 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 109 | <td><nobr><b><tt id='l2h-2721' xml:id='l2h-2721' class="method">release</tt></b>(</nobr></td> |
| 110 | <td><var></var>)</td></tr></table></dt> |
| 111 | <dd> |
| 112 | Release a lock. |
| 113 | |
| 114 | <P> |
| 115 | When the lock is locked, reset it to unlocked, and return. If |
| 116 | any other threads are blocked waiting for the lock to become |
| 117 | unlocked, allow exactly one of them to proceed. |
| 118 | |
| 119 | <P> |
| 120 | Do not call this method when the lock is unlocked. |
| 121 | |
| 122 | <P> |
| 123 | There is no return value. |
| 124 | </dl> |
| 125 | |
| 126 | <P> |
| 127 | |
| 128 | <DIV CLASS="navigation"> |
| 129 | <div class='online-navigation'> |
| 130 | <p></p><hr /> |
| 131 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 132 | <tr> |
| 133 | <td class='online-navigation'><a rel="prev" title="7.5 threading " |
| 134 | href="module-threading.html"><img src='../icons/previous.png' |
| 135 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 136 | <td class='online-navigation'><a rel="parent" title="7.5 threading " |
| 137 | href="module-threading.html"><img src='../icons/up.png' |
| 138 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 139 | <td class='online-navigation'><a rel="next" title="7.5.2 RLock Objects" |
| 140 | href="rlock-objects.html"><img src='../icons/next.png' |
| 141 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 142 | <td align="center" width="100%">Python Library Reference</td> |
| 143 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 144 | href="contents.html"><img src='../icons/contents.png' |
| 145 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 146 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 147 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 148 | <td class='online-navigation'><a rel="index" title="Index" |
| 149 | href="genindex.html"><img src='../icons/index.png' |
| 150 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 151 | </tr></table> |
| 152 | <div class='online-navigation'> |
| 153 | <b class="navlabel">Previous:</b> |
| 154 | <a class="sectref" rel="prev" href="module-threading.html">7.5 threading </A> |
| 155 | <b class="navlabel">Up:</b> |
| 156 | <a class="sectref" rel="parent" href="module-threading.html">7.5 threading </A> |
| 157 | <b class="navlabel">Next:</b> |
| 158 | <a class="sectref" rel="next" href="rlock-objects.html">7.5.2 RLock Objects</A> |
| 159 | </div> |
| 160 | </div> |
| 161 | <hr /> |
| 162 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 163 | </DIV> |
| 164 | <!--End of Navigation Panel--> |
| 165 | <ADDRESS> |
| 166 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 167 | </ADDRESS> |
| 168 | </BODY> |
| 169 | </HTML> |