perlthrtut.html
来自「perl教程」· HTML 代码 · 共 842 行 · 第 1/5 页
HTML
842 行
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>
</p>
<hr />
<h1><a name="what_kind_of_threads_are_perl_threads">What kind of threads are Perl threads?</a></h1>
<p>If you have experience with other thread implementations, you might
find that things aren't quite what you expect. It's very important to
remember when dealing with Perl threads that Perl Threads Are Not X
Threads, for all values of X. They aren't POSIX threads, or
DecThreads, or Java's Green threads, or Win32 threads. There are
similarities, and the broad concepts are the same, but if you start
looking for implementation details you're going to be either
disappointed or confused. Possibly both.</p>
<p>This is not to say that Perl threads are completely different from
everything that's ever come before--they're not. Perl's threading
model owes a lot to other thread models, especially POSIX. Just as
Perl is not C, though, Perl threads are not POSIX threads. So if you
find yourself looking for mutexes, or thread priorities, it's time to
step back a bit and think about what you want to do and how Perl can
do it.</p>
<p>However it is important to remember that Perl threads cannot magically
do things unless your operating systems threads allows it. So if your
system blocks the entire process on sleep(), Perl usually will as well.</p>
<p>Perl Threads Are Different.</p>
<p>
</p>
<hr />
<h1><a name="threadsafe_modules">Thread-Safe Modules</a></h1>
<p>The addition of threads has changed Perl's internals
substantially. There are implications for people who write
modules with XS code or external libraries. However, since perl data is
not shared among threads by default, Perl modules stand a high chance of
being thread-safe or can be made thread-safe easily. Modules that are not
tagged as thread-safe should be tested or code reviewed before being used
in production code.</p>
<p>Not all modules that you might use are thread-safe, and you should
always assume a module is unsafe unless the documentation says
otherwise. This includes modules that are distributed as part of the
core. Threads are a new feature, and even some of the standard
modules aren't thread-safe.</p>
<p>Even if a module is thread-safe, it doesn't mean that the module is optimized
to work well with threads. A module could possibly be rewritten to utilize
the new features in threaded Perl to increase performance in a threaded
environment.</p>
<p>If you're using a module that's not thread-safe for some reason, you
can protect yourself by using it from one, and only one thread at all.
If you need multiple threads to access such a module, you can use semaphores and
lots of programming discipline to control access to it. Semaphores
are covered in <a href="#basic_semaphores">Basic semaphores</a>.</p>
<p>See also <a href="#threadsafety_of_system_libraries">Thread-Safety of System Libraries</a>.</p>
<p>
</p>
<hr />
<h1><a name="thread_basics">Thread Basics</a></h1>
<p>The core <a href="../../lib/threads.html">the threads manpage</a> module provides the basic functions you need to write
threaded programs. In the following sections we'll cover the basics,
showing you what you need to do to create a threaded program. After
that, we'll go over some of the features of the <a href="../../lib/threads.html">the threads manpage</a> module that
make threaded programming easier.</p>
<p>
</p>
<h2><a name="basic_thread_support">Basic Thread Support</a></h2>
<p>Thread support is a Perl compile-time option - it's something that's
turned on or off when Perl is built at your site, rather than when
your programs are compiled. If your Perl wasn't compiled with thread
support enabled, then any attempt to use threads will fail.</p>
<p>Your programs can use the Config module to check whether threads are
enabled. If your program can't run without them, you can say something
like:</p>
<pre>
<span class="variable">$Config</span><span class="operator">{</span><span class="string">useithreads</span><span class="operator">}</span> <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"Recompile Perl with threads to run this program."</span><span class="operator">;</span>
</pre>
<p>A possibly-threaded program using a possibly-threaded module might
have code like this:</p>
<pre>
<span class="keyword">use</span> <span class="variable">Config</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">MyMod</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">BEGIN</span> <span class="operator">{</span>
<span class="keyword">if</span> <span class="operator">(</span><span class="variable">$Config</span><span class="operator">{</span><span class="string">useithreads</span><span class="operator">}</span><span class="operator">)</span> <span class="operator">{</span>
<span class="comment"># We have threads</span>
<span class="keyword">require</span> <span class="variable">MyMod_threaded</span><span class="operator">;</span>
<span class="variable">import</span> <span class="variable">MyMod_threaded</span><span class="operator">;</span>
<span class="operator">}</span> <span class="keyword">else</span> <span class="operator">{</span>
<span class="keyword">require</span> <span class="variable">MyMod_unthreaded</span><span class="operator">;</span>
<span class="variable">import</span> <span class="variable">MyMod_unthreaded</span><span class="operator">;</span>
<span class="operator">}</span>
<span class="operator">}</span>
</pre>
<p>Since code that runs both with and without threads is usually pretty
messy, it's best to isolate the thread-specific code in its own
module. In our example above, that's what MyMod_threaded is, and it's
only imported if we're running on a threaded Perl.</p>
<p>
</p>
<h2><a name="a_note_about_the_examples">A Note about the Examples</a></h2>
<p>Although thread support is considered to be stable, there are still a number
of quirks that may startle you when you try out any of the examples below.
In a real situation, care should be taken that all threads are finished
executing before the program exits. That care has <strong>not</strong> been taken in these
examples in the interest of simplicity. Running these examples "as is" will
produce error messages, usually caused by the fact that there are still
threads running when the program exits. You should not be alarmed by this.
Future versions of Perl may fix this problem.</p>
<p>
</p>
<h2><a name="creating_threads">Creating Threads</a></h2>
<p>The <a href="../../lib/threads.html">the threads manpage</a> package provides the tools you need to create new
threads. Like any other module, you need to tell Perl that you want to use
it; <code>use threads</code> imports all the pieces you need to create basic
threads.</p>
<p>The simplest, most straightforward way to create a thread is with new():</p>
<pre>
<span class="keyword">use</span> <span class="variable">threads</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">$thr</span> <span class="operator">=</span> <span class="variable">threads</span><span class="operator">-></span><span class="variable">new</span><span class="operator">(\&</span><span class="variable">sub1</span><span class="operator">);</span>
</pre>
<pre>
<span class="keyword">sub</span><span class="variable"> sub1 </span><span class="operator">{</span>
<span class="keyword">print</span> <span class="string">"In the thread\n"</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>The <code>new()</code> method takes a reference to a subroutine and creates a new
thread, which starts executing in the referenced subroutine. Control
then passes both to the subroutine and the caller.</p>
<p>If you need to, your program can pass parameters to the subroutine as
part of the thread startup. Just include the list of parameters as
part of the <code>threads::new</code> call, like this:</p>
<pre>
<span class="keyword">use</span> <span class="variable">threads</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">$Param3</span> <span class="operator">=</span> <span class="string">"foo"</span><span class="operator">;</span>
<span class="variable">$thr</span> <span class="operator">=</span> <span class="variable">threads</span><span class="operator">-></span><span class="variable">new</span><span class="operator">(\&</span><span class="variable">sub1</span><span class="operator">,</span> <span class="string">"Param 1"</span><span class="operator">,</span> <span class="string">"Param 2"</span><span class="operator">,</span> <span class="variable">$Param3</span><span class="operator">);</span>
<span class="variable">$thr</span> <span class="operator">=</span> <span class="variable">threads</span><span class="operator">-></span><span class="variable">new</span><span class="operator">(\&</span><span class="variable">sub1</span><span class="operator">,</span> <span class="variable">@ParamList</span><span class="operator">);</span>
<span class="variable">$thr</span> <span class="operator">=</span> <span class="variable">threads</span><span class="operator">-></span><span class="variable">new</span><span class="operator">(\&</span><span class="variable">sub1</span><span class="operator">,</span> <span class="string">qw(Param1 Param2 Param3)</span><span class="operator">);</span>
</pre>
<pre>
<span class="keyword">sub</span><span class="variable"> sub1 </span><span class="operator">{</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?