perlothrtut.html
来自「perl教程」· HTML 代码 · 共 903 行 · 第 1/5 页
HTML
903 行
</p>
<hr />
<h1><a name="native_threads">Native threads</a></h1>
<p>There are several different ways to implement threads on a system. How
threads are implemented depends both on the vendor and, in some cases,
the version of the operating system. Often the first implementation
will be relatively simple, but later versions of the OS will be more
sophisticated.</p>
<p>While the information in this section is useful, it's not necessary,
so you can skip it if you don't feel up to it.</p>
<p>There are three basic categories of threads-user-mode threads, kernel
threads, and multiprocessor kernel threads.</p>
<p>User-mode threads are threads that live entirely within a program and
its libraries. In this model, the OS knows nothing about threads. As
far as it's concerned, your process is just a process.</p>
<p>This is the easiest way to implement threads, and the way most OSes
start. The big disadvantage is that, since the OS knows nothing about
threads, if one thread blocks they all do. Typical blocking activities
include most system calls, most I/O, and things like sleep().</p>
<p>Kernel threads are the next step in thread evolution. The OS knows
about kernel threads, and makes allowances for them. The main
difference between a kernel thread and a user-mode thread is
blocking. With kernel threads, things that block a single thread don't
block other threads. This is not the case with user-mode threads,
where the kernel blocks at the process level and not the thread level.</p>
<p>This is a big step forward, and can give a threaded program quite a
performance boost over non-threaded programs. Threads that block
performing I/O, for example, won't block threads that are doing other
things. Each process still has only one thread running at once,
though, regardless of how many CPUs a system might have.</p>
<p>Since kernel threading can interrupt a thread at any time, they will
uncover some of the implicit locking assumptions you may make in your
program. For example, something as simple as <a href="../../lib/Pod/perlvar.html#item__a"><code>$a = $a + 2</code></a> can behave
unpredictably with kernel threads if $a is visible to other
threads, as another thread may have changed $a between the time it
was fetched on the right hand side and the time the new value is
stored.</p>
<p>Multiprocessor Kernel Threads are the final step in thread
support. With multiprocessor kernel threads on a machine with multiple
CPUs, the OS may schedule two or more threads to run simultaneously on
different CPUs.</p>
<p>This can give a serious performance boost to your threaded program,
since more than one thread will be executing at the same time. As a
tradeoff, though, any of those nagging synchronization issues that
might not have shown with basic kernel threads will appear with a
vengeance.</p>
<p>In addition to the different levels of OS involvement in threads,
different OSes (and different thread implementations for a particular
OS) allocate CPU cycles to threads in different ways.</p>
<p>Cooperative multitasking systems have running threads give up control
if one of two things happen. If a thread calls a yield function, it
gives up control. It also gives up control if the thread does
something that would cause it to block, such as perform I/O. In a
cooperative multitasking implementation, one thread can starve all the
others for CPU time if it so chooses.</p>
<p>Preemptive multitasking systems interrupt threads at regular intervals
while the system decides which thread should run next. In a preemptive
multitasking system, one thread usually won't monopolize the CPU.</p>
<p>On some systems, there can be cooperative and preemptive threads
running simultaneously. (Threads running with realtime priorities
often behave cooperatively, for example, while threads running at
normal priorities behave preemptively.)</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>
</p>
<hr />
<h1><a name="threadsafe_modules">Threadsafe Modules</a></h1>
<p>The addition of threads has changed Perl's internals
substantially. There are implications for people who write
modules--especially modules with XS code or external libraries. While
most modules won't encounter any problems, modules that aren't
explicitly tagged as thread-safe should be tested 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 beta feature, and even some of the standard
modules aren't thread-safe.</p>
<p>If you're using a module that's not thread-safe for some reason, you
can protect yourself by using semaphores and lots of programming
discipline to control access to the module. Semaphores are covered
later in the article. Perl Threads Are Different</p>
<p>
</p>
<hr />
<h1><a name="thread_basics">Thread Basics</a></h1>
<p>The core Thread 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 Thread 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>Remember that the threading support in 5.005 is in beta release, and
should be treated as such. You should expect that it may not function
entirely properly, and the thread interface may well change some
before it is a fully supported, production release. The beta version
shouldn't be used for mission-critical projects. Having said that,
threaded Perl is pretty nifty, and worth a look.</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">usethreads</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">if</span> <span class="operator">(</span><span class="variable">$Config</span><span class="operator">{</span><span class="string">usethreads</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>
</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="creating_threads">Creating Threads</a></h2>
<p>The Thread package provides the tools you need to create new
threads. Like any other module, you need to tell Perl you want to use
it; use Thread imports all the pieces you need to create basic
threads.</p>
<p>The simplest, straightforward way to create a thread is with new():</p>
<pre>
<span class="keyword">use</span> <span class="variable">Thread</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">$thr</span> <span class="operator">=</span> <span class="variable">new</span> <span class="variable">Thread</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>Thread::new</code> call, like this:</p>
<pre>
<span class="keyword">use</span> <span class="variable">Thread</span><span class="operator">;</span>
<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">new</span> <span class="variable">Thread</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">new</span> <span class="variable">Thread</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">new</span> <span class="variable">Thread</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>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?