perlothrtut.html
来自「perl教程」· HTML 代码 · 共 903 行 · 第 1/5 页
HTML
903 行
<?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>perlothrtut - old 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>perlothrtut - old 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="#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="#native_threads">Native threads</a></li>
<li><a href="#what_kind_of_threads_are_perl_threads">What kind of threads are perl threads?</a></li>
<li><a href="#threadsafe_modules">Threadsafe 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="#creating_threads">Creating Threads</a></li>
<li><a href="#giving_up_control">Giving up control</a></li>
<li><a href="#waiting_for_a_thread_to_exit">Waiting For A Thread To Exit</a></li>
<li><a href="#errors_in_threads">Errors In Threads</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_pitfall__races">Thread Pitfall: Races</a></li>
<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="#thread_pitfall__deadlocks">Thread Pitfall: Deadlocks</a></li>
<li><a href="#queues__passing_data_around">Queues: Passing Data Around</a></li>
</ul>
<li><a href="#threads_and_code">Threads And Code</a></li>
<ul>
<li><a href="#semaphores__synchronizing_data_access">Semaphores: Synchronizing Data Access</a></li>
<li><a href="#attributes__restricting_access_to_subroutines">Attributes: Restricting Access To Subroutines</a></li>
<li><a href="#subroutine_locks">Subroutine Locks</a></li>
<li><a href="#methods">Methods</a></li>
<li><a href="#locking_a_subroutine">Locking A Subroutine</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="#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>perlothrtut - old tutorial on threads in Perl</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p><strong>WARNING</strong>:
This tutorial describes the old-style thread model that was introduced in
release 5.005. This model is now deprecated, and will be removed, probably
in version 5.10. The interfaces described here were considered
experimental, and are likely to be buggy.</p>
<p>For information about the new interpreter threads ("ithreads") model, see
the <em>perlthrtut</em> tutorial, and the <a href="../../lib/threads.html">the threads manpage</a> and <a href="../../lib/threads/shared.html">the threads::shared manpage</a>
modules.</p>
<p>You are strongly encouraged to migrate any existing threads code to the
new model as soon as possible.</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.005, 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 `boss' thread and one or more
`worker' 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
essentially the same thing to different pieces of data. It closely
mirrors classical parallel processing and vector processors, where a
large array of processors do the exact same thing to many pieces of
data.</p>
<p>This model is particularly useful if the system running the program
will distribute multiple threads across different processors. It can
also be useful in ray tracing or rendering engines, where the
individual threads can pass on interim results to give the user visual
feedback.</p>
<p>
</p>
<h2><a name="pipeline">Pipeline</a></h2>
<p>The pipeline model divides up a task into a series of steps, and
passes the results of one step on to the thread processing the
next. Each thread does one thing to each piece of data and passes the
results to the next thread in line.</p>
<p>This model makes the most sense if you have multiple processors so two
or more threads will be executing in parallel, though it can often
make sense in other contexts as well. It tends to keep the individual
tasks small and simple, as well as allowing some parts of the pipeline
to block (on I/O or system calls, for example) while other parts keep
going. If you're running different parts of the pipeline on different
processors you may also take advantage of the caches on each
processor.</p>
<p>This model is also handy for a form of recursive programming where,
rather than having a subroutine call itself, it instead creates
another thread. Prime and Fibonacci generators both map well to this
form of the pipeline model. (A version of a prime number generator is
presented later on.)</p>
<p>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?