Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / module-threading.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="module-dummythread.html" />
<link rel="prev" href="module-thread.html" />
<link rel="parent" href="someos.html" />
<link rel="next" href="lock-objects.html" />
<meta name='aesop' content='information' />
<title>7.5 threading -- Higher-level threading interface</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.4 thread "
href="module-thread.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. Optional Operating System"
href="someos.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.1 Lock Objects"
href="lock-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="module-thread.html">7.4 thread </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="someos.html">7. Optional Operating System</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="lock-objects.html">7.5.1 Lock Objects</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION009500000000000000000">
7.5 <tt class="module">threading</tt> --
Higher-level threading interface</A>
</H1>
<P>
<A NAME="module-threading"></A>
<P>
This module constructs higher-level threading interfaces on top of the
lower level <tt class="module"><a href="module-thread.html">thread</a></tt> module.
<P>
The <tt class="module"><a href="module-dummythreading.html">dummy_threading</a></tt> module is provided for
situations where <tt class="module">threading</tt> cannot be used because
<tt class="module"><a href="module-thread.html">thread</a></tt> is missing.
<P>
This module defines the following functions and objects:
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-2704' xml:id='l2h-2704' class="function">activeCount</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Return the number of currently active <tt class="class">Thread</tt> objects.
The returned count is equal to the length of the list returned by
<tt class="function">enumerate()</tt>.
A function that returns the number of currently active threads.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-2705' xml:id='l2h-2705' class="function">Condition</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
A factory function that returns a new condition variable object.
A condition variable allows one or more threads to wait until they
are notified by another thread.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-2706' xml:id='l2h-2706' class="function">currentThread</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Return the current <tt class="class">Thread</tt> object, corresponding to the
caller's thread of control. If the caller's thread of control was not
created through the
<tt class="module">threading</tt> module, a dummy thread object with limited functionality
is returned.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-2707' xml:id='l2h-2707' class="function">enumerate</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
Return a list of all currently active <tt class="class">Thread</tt> objects.
The list includes daemonic threads, dummy thread objects created
by <tt class="function">currentThread()</tt>, and the main thread. It excludes terminated
threads and threads that have not yet been started.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-2708' xml:id='l2h-2708' class="function">Event</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
A factory function that returns a new event object. An event manages
a flag that can be set to true with the <tt class="method">set()</tt> method and
reset to false with the <tt class="method">clear()</tt> method. The <tt class="method">wait()</tt>
method blocks until the flag is true.
</dl>
<P>
<dl><dt><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2709' xml:id='l2h-2709' class="class">local</tt></b>
<dd>
A class that represents thread-local data. Thread-local data are data
whose values are thread specific. To manage thread-local data, just
create an instance of <tt class="class">local</tt> (or a subclass) and store
attributes on it:
<P>
<div class="verbatim"><pre>
mydata = threading.local()
mydata.x = 1
</pre></div>
<P>
The instance's values will be different for separate threads.
<P>
For more details and extensive examples, see the documentation string
of the <tt class="module">_threading_local</tt> module.
<P>
<span class="versionnote">New in version 2.4.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-2710' xml:id='l2h-2710' class="function">Lock</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
A factory function that returns a new primitive lock object. Once
a thread has acquired it, subsequent attempts to acquire it block,
until it is released; any thread may release it.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-2711' xml:id='l2h-2711' class="function">RLock</tt></b>(</nobr></td>
<td><var></var>)</td></tr></table></dt>
<dd>
A factory function that returns a new reentrant lock object.
A reentrant lock must be released by the thread that acquired it.
Once a thread has acquired a reentrant lock, the same thread may
acquire it again without blocking; the thread must release it once
for each time it has acquired it.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-2712' xml:id='l2h-2712' class="function">Semaphore</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>value</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
A factory function that returns a new semaphore object. A
semaphore manages a counter representing the number of <tt class="method">release()</tt>
calls minus the number of <tt class="method">acquire()</tt> calls, plus an initial value.
The <tt class="method">acquire()</tt> method blocks if necessary until it can return
without making the counter negative. If not given, <var>value</var> defaults to
1.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-2713' xml:id='l2h-2713' class="function">BoundedSemaphore</tt></b>(</nobr></td>
<td><var></var><big>[</big><var>value</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
A factory function that returns a new bounded semaphore object. A bounded
semaphore checks to make sure its current value doesn't exceed its initial
value. If it does, <tt class="exception">ValueError</tt> is raised. In most situations
semaphores are used to guard resources with limited capacity. If the
semaphore is released too many times it's a sign of a bug. If not given,
<var>value</var> defaults to 1.
</dl>
<P>
<dl><dt><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2714' xml:id='l2h-2714' class="class">Thread</tt></b>
<dd>
A class that represents a thread of control. This class can be safely
subclassed in a limited fashion.
</dl>
<P>
<dl><dt><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2715' xml:id='l2h-2715' class="class">Timer</tt></b>
<dd>
A thread that executes a function after a specified interval has passed.
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-2716' xml:id='l2h-2716' class="function">settrace</tt></b>(</nobr></td>
<td><var>func</var>)</td></tr></table></dt>
<dd>
Set a trace function<a id='l2h-2718' xml:id='l2h-2718'></a> for all threads started
from the <tt class="module">threading</tt> module. The <var>func</var> will be passed to
<tt class="function">sys.settrace()</tt> for each thread, before its <tt class="method">run()</tt>
method is called.
<span class="versionnote">New in version 2.3.</span>
</dl>
<P>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
<td><nobr><b><tt id='l2h-2717' xml:id='l2h-2717' class="function">setprofile</tt></b>(</nobr></td>
<td><var>func</var>)</td></tr></table></dt>
<dd>
Set a profile function<a id='l2h-2719' xml:id='l2h-2719'></a> for all threads started
from the <tt class="module">threading</tt> module. The <var>func</var> will be passed to
<tt class="function">sys.setprofile()</tt> for each thread, before its <tt class="method">run()</tt>
method is called.
<span class="versionnote">New in version 2.3.</span>
</dl>
<P>
Detailed interfaces for the objects are documented below.
<P>
The design of this module is loosely based on Java's threading model.
However, where Java makes locks and condition variables basic behavior
of every object, they are separate objects in Python. Python's <tt class="class">Thread</tt>
class supports a subset of the behavior of Java's Thread class;
currently, there are no priorities, no thread groups, and threads
cannot be destroyed, stopped, suspended, resumed, or interrupted. The
static methods of Java's Thread class, when implemented, are mapped to
module-level functions.
<P>
All of the methods described below are executed atomically.
<P>
<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
<UL CLASS="ChildLinks">
<LI><A href="lock-objects.html">7.5.1 Lock Objects</a>
<LI><A href="rlock-objects.html">7.5.2 RLock Objects</a>
<LI><A href="condition-objects.html">7.5.3 Condition Objects</a>
<LI><A href="semaphore-objects.html">7.5.4 Semaphore Objects</a>
<UL>
<LI><A href="semaphore-examples.html">7.5.4.1 <tt class="class">Semaphore</tt> Example</a>
</ul>
<LI><A href="event-objects.html">7.5.5 Event Objects</a>
<LI><A href="thread-objects.html">7.5.6 Thread Objects</a>
<LI><A href="timer-objects.html">7.5.7 Timer Objects</a>
</ul>
<!--End of Table of Child-Links-->
</div>
<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.4 thread "
href="module-thread.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. Optional Operating System"
href="someos.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.1 Lock Objects"
href="lock-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="module-thread.html">7.4 thread </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="someos.html">7. Optional Operating System</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="lock-objects.html">7.5.1 Lock 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>