perlthrtut.html

来自「perl教程」· HTML 代码 · 共 842 行 · 第 1/5 页

HTML
842
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perlthrtut - tutorial on threads in Perl</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>perlthrtut - tutorial on threads in Perl</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<li><a href="#status">Status</a></li>
	<li><a href="#what_is_a_thread_anyway">What Is A Thread Anyway?</a></li>
	<li><a href="#threaded_program_models">Threaded Program Models</a></li>
	<ul>

		<li><a href="#boss_worker">Boss/Worker</a></li>
		<li><a href="#work_crew">Work Crew</a></li>
		<li><a href="#pipeline">Pipeline</a></li>
	</ul>

	<li><a href="#what_kind_of_threads_are_perl_threads">What kind of threads are Perl threads?</a></li>
	<li><a href="#threadsafe_modules">Thread-Safe Modules</a></li>
	<li><a href="#thread_basics">Thread Basics</a></li>
	<ul>

		<li><a href="#basic_thread_support">Basic Thread Support</a></li>
		<li><a href="#a_note_about_the_examples">A Note about the Examples</a></li>
		<li><a href="#creating_threads">Creating Threads</a></li>
		<li><a href="#waiting_for_a_thread_to_exit">Waiting For A Thread To Exit</a></li>
		<li><a href="#ignoring_a_thread">Ignoring A Thread</a></li>
	</ul>

	<li><a href="#threads_and_data">Threads And Data</a></li>
	<ul>

		<li><a href="#shared_and_unshared_data">Shared And Unshared Data</a></li>
		<li><a href="#thread_pitfalls__races">Thread Pitfalls: Races</a></li>
	</ul>

	<li><a href="#synchronization_and_control">Synchronization and control</a></li>
	<ul>

		<li><a href="#controlling_access__lock__">Controlling access: <a href="../../lib/Pod/perlfunc.html#item_lock"><code>lock()</code></a></a></li>
		<li><a href="#a_thread_pitfall__deadlocks">A Thread Pitfall: Deadlocks</a></li>
		<li><a href="#queues__passing_data_around">Queues: Passing Data Around</a></li>
		<li><a href="#semaphores__synchronizing_data_access">Semaphores: Synchronizing Data Access</a></li>
		<li><a href="#basic_semaphores">Basic semaphores</a></li>
		<li><a href="#advanced_semaphores">Advanced Semaphores</a></li>
		<li><a href="#cond_wait___and_cond_signal__"><code>cond_wait()</code> and <code>cond_signal()</code></a></li>
		<li><a href="#giving_up_control">Giving up control</a></li>
	</ul>

	<li><a href="#general_thread_utility_routines">General Thread Utility Routines</a></li>
	<ul>

		<li><a href="#what_thread_am_i_in">What Thread Am I In?</a></li>
		<li><a href="#thread_ids">Thread IDs</a></li>
		<li><a href="#are_these_threads_the_same">Are These Threads The Same?</a></li>
		<li><a href="#what_threads_are_running">What Threads Are Running?</a></li>
	</ul>

	<li><a href="#a_complete_example">A Complete Example</a></li>
	<li><a href="#different_implementations_of_threads">Different implementations of threads</a></li>
	<li><a href="#performance_considerations">Performance considerations</a></li>
	<li><a href="#processscope_changes">Process-scope Changes</a></li>
	<li><a href="#threadsafety_of_system_libraries">Thread-Safety of System Libraries</a></li>
	<li><a href="#conclusion">Conclusion</a></li>
	<li><a href="#bibliography">Bibliography</a></li>
	<ul>

		<li><a href="#introductory_texts">Introductory Texts</a></li>
		<li><a href="#osrelated_references">OS-Related References</a></li>
		<li><a href="#other_references">Other References</a></li>
	</ul>

	<li><a href="#acknowledgements">Acknowledgements</a></li>
	<li><a href="#author">AUTHOR</a></li>
	<li><a href="#copyrights">Copyrights</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlthrtut - tutorial on threads in Perl</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p><strong>NOTE</strong>: this tutorial describes the new Perl threading flavour
introduced in Perl 5.6.0 called interpreter threads, or <strong>ithreads</strong>
for short.  In this model each thread runs in its own Perl interpreter,
and any data sharing between threads must be explicit.</p>
<p>There is another older Perl threading flavour called the 5.005 model,
unsurprisingly for 5.005 versions of Perl.  The old model is known to
have problems, deprecated, and will probably be removed around release
5.10. You are strongly encouraged to migrate any existing 5.005
threads code to the new model as soon as possible.</p>
<p>You can see which (or neither) threading flavour you have by
running <code>perl -V</code> and looking at the <code>Platform</code> section.
If you have <code>useithreads=define</code> you have ithreads, if you
have <code>use5005threads=define</code> you have 5.005 threads.
If you have neither, you don't have any thread support built in.
If you have both, you are in trouble.</p>
<p>The user-level interface to the 5.005 threads was via the <em>Threads</em>
class, while ithreads uses the <a href="../../lib/threads.html">the threads manpage</a> class. Note the change in case.</p>
<p>
</p>
<hr />
<h1><a name="status">Status</a></h1>
<p>The ithreads code has been available since Perl 5.6.0, and is considered
stable. The user-level interface to ithreads (the <a href="../../lib/threads.html">the threads manpage</a> classes)
appeared in the 5.8.0 release, and as of this time is considered stable
although it should be treated with caution as with all new features.</p>
<p>
</p>
<hr />
<h1><a name="what_is_a_thread_anyway">What Is A Thread Anyway?</a></h1>
<p>A thread is a flow of control through a program with a single
execution point.</p>
<p>Sounds an awful lot like a process, doesn't it? Well, it should.
Threads are one of the pieces of a process.  Every process has at least
one thread and, up until now, every process running Perl had only one
thread.  With 5.8, though, you can create extra threads.  We're going
to show you how, when, and why.</p>
<p>
</p>
<hr />
<h1><a name="threaded_program_models">Threaded Program Models</a></h1>
<p>There are three basic ways that you can structure a threaded
program.  Which model you choose depends on what you need your program
to do.  For many non-trivial threaded programs you'll need to choose
different models for different pieces of your program.</p>
<p>
</p>
<h2><a name="boss_worker">Boss/Worker</a></h2>
<p>The boss/worker model usually has one &quot;boss&quot; thread and one or more
&quot;worker&quot; threads.  The boss thread gathers or generates tasks that need
to be done, then parcels those tasks out to the appropriate worker
thread.</p>
<p>This model is common in GUI and server programs, where a main thread
waits for some event and then passes that event to the appropriate
worker threads for processing.  Once the event has been passed on, the
boss thread goes back to waiting for another event.</p>
<p>The boss thread does relatively little work.  While tasks aren't
necessarily performed faster than with any other method, it tends to
have the best user-response times.</p>
<p>
</p>
<h2><a name="work_crew">Work Crew</a></h2>
<p>In the work crew model, several threads are created that do

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?