Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / lib / condition-objects.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="semaphore-objects.html" />
<link rel="prev" href="rlock-objects.html" />
<link rel="parent" href="module-threading.html" />
<link rel="next" href="semaphore-objects.html" />
<meta name='aesop' content='information' />
<title>7.5.3 Condition Objects </title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="7.5.2 RLock Objects"
href="rlock-objects.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="7.5 threading "
href="module-threading.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="7.5.4 Semaphore Objects"
href="semaphore-objects.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="rlock-objects.html">7.5.2 RLock Objects</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-threading.html">7.5 threading </A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="semaphore-objects.html">7.5.4 Semaphore Objects</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H2><A NAME="SECTION009530000000000000000"></A><A NAME="condition-objects"></A>
<BR>
7.5.3 Condition Objects
</H2>
<P>
A condition variable is always associated with some kind of lock;
this can be passed in or one will be created by default. (Passing
one in is useful when several condition variables must share the
same lock.)
<P>
A condition variable has <tt class="method">acquire()</tt> and <tt class="method">release()</tt>
methods that call the corresponding methods of the associated lock.
It also has a <tt class="method">wait()</tt> method, and <tt class="method">notify()</tt> and
<tt class="method">notifyAll()</tt> methods. These three must only be called when
the calling thread has acquired the lock.
<P>
The <tt class="method">wait()</tt> method releases the lock, and then blocks until it
is awakened by a <tt class="method">notify()</tt> or <tt class="method">notifyAll()</tt> call for
the same condition variable in another thread. Once awakened, it
re-acquires the lock and returns. It is also possible to specify a
timeout.
<P>
The <tt class="method">notify()</tt> method wakes up one of the threads waiting for
the condition variable, if any are waiting. The <tt class="method">notifyAll()</tt>
method wakes up all threads waiting for the condition variable.
<P>
Note: the <tt class="method">notify()</tt> and <tt class="method">notifyAll()</tt> methods don't
release the lock; this means that the thread or threads awakened will
not return from their <tt class="method">wait()</tt> call immediately, but only when
the thread that called <tt class="method">notify()</tt> or <tt class="method">notifyAll()</tt>
finally relinquishes ownership of the lock.
<P>
Tip: the typical programming style using condition variables uses the
lock to synchronize access to some shared state; threads that are
interested in a particular change of state call <tt class="method">wait()</tt>
repeatedly until they see the desired state, while threads that modify
the state call <tt class="method">notify()</tt> or <tt class="method">notifyAll()</tt> when they
change the state in such a way that it could possibly be a desired
state for one of the waiters. For example, the following code is a
generic producer-consumer situation with unlimited buffer capacity:
<P>
<div class="verbatim"><pre>
# Consume one item
cv.acquire()
while not an_item_is_available():
cv.wait()
get_an_available_item()
cv.release()
# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
</pre></div>
<P>
To choose between <tt class="method">notify()</tt> and <tt class="method">notifyAll()</tt>, consider
whether one state change can be interesting for only one or several
waiting threads. E.g. in a typical producer-consumer situation,
adding one item to the buffer only needs to wake up one consumer
thread.
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2724' xml:id='l2h-2724' class="class">Condition</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>lock</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
If the <var>lock</var> argument is given and not <code>None</code>, it must be a
<tt class="class">Lock</tt> or <tt class="class">RLock</tt> object, and it is used as the underlying
lock. Otherwise, a new <tt class="class">RLock</tt> object is created and used as
the underlying lock.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-2725' xml:id='l2h-2725' class="method">acquire</tt></b>(</nobr></td>
<td><var>*args</var>)</td></tr></table></dt>
<dd>
Acquire the underlying lock.
This method calls the corresponding method on the underlying
lock; the return value is whatever that method returns.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-2726' xml:id='l2h-2726' class="method">release</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Release the underlying lock.
This method calls the corresponding method on the underlying
lock; there is no return value.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-2727' xml:id='l2h-2727' class="method">wait</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>timeout</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Wait until notified or until a timeout occurs.
This must only be called when the calling thread has acquired the
lock.
<P>
This method releases the underlying lock, and then blocks until it is
awakened by a <tt class="method">notify()</tt> or <tt class="method">notifyAll()</tt> call for the
same condition variable in another thread, or until the optional
timeout occurs. Once awakened or timed out, it re-acquires the lock
and returns.
<P>
When the <var>timeout</var> argument is present and not <code>None</code>, it
should be a floating point number specifying a timeout for the
operation in seconds (or fractions thereof).
<P>
When the underlying lock is an <tt class="class">RLock</tt>, it is not released using
its <tt class="method">release()</tt> method, since this may not actually unlock the
lock when it was acquired multiple times recursively. Instead, an
internal interface of the <tt class="class">RLock</tt> class is used, which really
unlocks it even when it has been recursively acquired several times.
Another internal interface is then used to restore the recursion level
when the lock is reacquired.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-2728' xml:id='l2h-2728' class="method">notify</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Wake up a thread waiting on this condition, if any.
This must only be called when the calling thread has acquired the
lock.
<P>
This method wakes up one of the threads waiting for the condition
variable, if any are waiting; it is a no-op if no threads are waiting.
<P>
The current implementation wakes up exactly one thread, if any are
waiting. However, it's not safe to rely on this behavior. A future,
optimized implementation may occasionally wake up more than one
thread.
<P>
Note: the awakened thread does not actually return from its
<tt class="method">wait()</tt> call until it can reacquire the lock. Since
<tt class="method">notify()</tt> does not release the lock, its caller should.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-2729' xml:id='l2h-2729' class="method">notifyAll</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Wake up all threads waiting on this condition. This method acts like
<tt class="method">notify()</tt>, but wakes up all waiting threads instead of one.
</dl>
<P>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="7.5.2 RLock Objects"
href="rlock-objects.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="7.5 threading "
href="module-threading.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="7.5.4 Semaphore Objects"
href="semaphore-objects.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="rlock-objects.html">7.5.2 RLock Objects</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-threading.html">7.5 threading </A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="semaphore-objects.html">7.5.4 Semaphore Objects</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>