<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>7.5.1 Lock Objects </TITLE>
<META NAME="description" CONTENT="7.5.1 Lock Objects ">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="next" HREF="rlock-objects.html">
<LINK REL="previous" HREF="module-threading.html">
<LINK REL="up" HREF="module-threading.html">
<LINK REL="next" HREF="rlock-objects.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="rlock-objects.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="module-threading.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-threading.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="rlock-objects.html">7.5.2 RLock Objects</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="module-threading.html">7.5 threading  </A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-threading.html">7.5 threading  </A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION009510000000000000000"></A>
<BR>
7.5.1 Lock Objects 
</H2>

<P>
A primitive lock is a synchronization primitive that is not owned
by a particular thread when locked.  In Python, it is currently
the lowest level synchronization primitive available, implemented
directly by the <tt class=module><a href="module-thread.html">thread</a></tt> extension module.

<P>
A primitive lock is in one of two states, ``locked'' or ``unlocked''.
It is created in the unlocked state.  It has two basic methods,
<tt class=method>acquire()</tt> and <tt class=method>release()</tt>.  When the state is
unlocked, <tt class=method>acquire()</tt> changes the state to locked and returns
immediately.  When the state is locked, <tt class=method>acquire()</tt> blocks
until a call to <tt class=method>release()</tt> in another thread changes it to
unlocked, then the <tt class=method>acquire()</tt> call resets it to locked and
returns.  The <tt class=method>release()</tt> method should only be called in the
locked state; it changes the state to unlocked and returns
immediately.  When more than one thread is blocked in
<tt class=method>acquire()</tt> waiting for the state to turn to unlocked, only one
thread proceeds when a <tt class=method>release()</tt> call resets the state to
unlocked; which one of the waiting threads proceeds is not defined,
and may vary across implementations.

<P>
All methods are executed atomically.

<P>
<dl><dt><b><a name="l2h-1348"><tt class=method>acquire</tt></a></b> (<big>[</big><var>blocking<tt> = 1</tt></var><big>]</big>)
<dd>
Acquire a lock, blocking or non-blocking.

<P>
When invoked without arguments, block until the lock is
unlocked, then set it to locked, and return.  There is no
return value in this case.

<P>
When invoked with the <var>blocking</var> argument set to true, do the
same thing as when called without arguments, and return true.

<P>
When invoked with the <var>blocking</var> argument set to false, do not
block.  If a call without an argument would block, return false
immediately; otherwise, do the same thing as when called
without arguments, and return true.
</dl>

<P>
<dl><dt><b><a name="l2h-1349"><tt class=method>release</tt></a></b> ()
<dd>
Release a lock.

<P>
When the lock is locked, reset it to unlocked, and return.  If
any other threads are blocked waiting for the lock to become
unlocked, allow exactly one of them to proceed.

<P>
Do not call this method when the lock is unlocked.

<P>
There is no return value.
</dl>

<P>
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="rlock-objects.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="module-threading.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-threading.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="rlock-objects.html">7.5.2 RLock Objects</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="module-threading.html">7.5 threading  </A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-threading.html">7.5 threading  </A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
