perlothrtut.1

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· 1 代码 · 共 1,197 行 · 第 1/4 页

1
1,197
字号
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...de Sp \" Vertical space (when we can't use .PP).if t .sp .5v.if n .sp...de Vb \" Begin verbatim text.ft CW.nf.ne \\$1...de Ve \" End verbatim text.ft R.fi...\" Set up some character translations and predefined strings.  \*(-- will.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left.\" double quote, and \*(R" will give a right double quote.  \*(C+ will.\" give a nicer C++.  Capital omega is used to do unbreakable dashes and.\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,.\" nothing in troff, for use with C<>..tr \(*W-.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'.ie n \{\.    ds -- \(*W-.    ds PI pi.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch.    ds L" "".    ds R" "".    ds C` "".    ds C' ""'br\}.el\{\.    ds -- \|\(em\|.    ds PI \(*p.    ds L" ``.    ds R" '''br\}.\".\" Escape single quotes in literal strings from groff's Unicode transform..ie \n(.g .ds Aq \(aq.el       .ds Aq '.\".\" If the F register is turned on, we'll generate index entries on stderr for.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index.\" entries marked with X<> in POD.  Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\.    de IX.    tm Index:\\$1\t\\n%\t"\\$2"...    nr % 0.    rr F.\}.el \{\.    de IX...\}.\".\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2)..\" Fear.  Run.  Save yourself.  No user-serviceable parts..    \" fudge factors for nroff and troff.if n \{\.    ds #H 0.    ds #V .8m.    ds #F .3m.    ds #[ \f1.    ds #] \fP.\}.if t \{\.    ds #H ((1u-(\\\\n(.fu%2u))*.13m).    ds #V .6m.    ds #F 0.    ds #[ \&.    ds #] \&.\}.    \" simple accents for nroff and troff.if n \{\.    ds ' \&.    ds ` \&.    ds ^ \&.    ds , \&.    ds ~ ~.    ds /.\}.if t \{\.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u".    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'.\}.    \" troff and (daisy-wheel) nroff accents.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'.ds 8 \h'\*(#H'\(*b\h'-\*(#H'.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#].ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#].ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#].ds ae a\h'-(\w'a'u*4/10)'e.ds Ae A\h'-(\w'A'u*4/10)'E.    \" corrections for vroff.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'.    \" for low resolution devices (crt and lpr).if \n(.H>23 .if \n(.V>19 \\{\.    ds : e.    ds 8 ss.    ds o a.    ds d- d\h'-1'\(ga.    ds D- D\h'-1'\(hy.    ds th \o'bp'.    ds Th \o'LP'.    ds ae ae.    ds Ae AE.\}.rm #[ #] #H #V #F C.\" ========================================================================.\".IX Title "PERLOTHRTUT 1".TH PERLOTHRTUT 1 "2007-12-18" "perl v5.10.0" "Perl Programmers Reference Guide".\" For nroff, turn off justification.  Always turn off hyphenation; it makes.\" way too many mistakes in technical documents..if n .ad l.nh.SH "NAME"perlothrtut \- old tutorial on threads in Perl.SH "DESCRIPTION".IX Header "DESCRIPTION"\&\fB\s-1WARNING\s0\fR:This tutorial describes the old-style thread model that was introduced inrelease 5.005. This model is deprecated, and has been removedfor version 5.10. The interfaces described here were consideredexperimental, and are likely to be buggy..PPFor information about the new interpreter threads (\*(L"ithreads\*(R") model, seethe \fIperlthrtut\fR tutorial, and the threads and threads::sharedmodules..PPYou are strongly encouraged to migrate any existing threads code to thenew model as soon as possible..SH "What Is A Thread Anyway?".IX Header "What Is A Thread Anyway?"A thread is a flow of control through a program with a singleexecution point..PPSounds 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 leastone thread and, up until now, every process running Perl had only onethread.  With 5.005, though, you can create extra threads.  We're goingto show you how, when, and why..SH "Threaded Program Models".IX Header "Threaded Program Models"There are three basic ways that you can structure a threadedprogram.  Which model you choose depends on what you need your programto do.  For many non-trivial threaded programs you'll need to choosedifferent models for different pieces of your program..Sh "Boss/Worker".IX Subsection "Boss/Worker"The boss/worker model usually has one `boss' thread and one or more`worker' threads.  The boss thread gathers or generates tasks that needto be done, then parcels those tasks out to the appropriate workerthread..PPThis model is common in \s-1GUI\s0 and server programs, where a main threadwaits for some event and then passes that event to the appropriateworker threads for processing.  Once the event has been passed on, theboss thread goes back to waiting for another event..PPThe boss thread does relatively little work.  While tasks aren'tnecessarily performed faster than with any other method, it tends tohave the best user-response times..Sh "Work Crew".IX Subsection "Work Crew"In the work crew model, several threads are created that doessentially the same thing to different pieces of data.  It closelymirrors classical parallel processing and vector processors, where alarge array of processors do the exact same thing to many pieces ofdata..PPThis model is particularly useful if the system running the programwill distribute multiple threads across different processors.  It canalso be useful in ray tracing or rendering engines, where theindividual threads can pass on interim results to give the user visualfeedback..Sh "Pipeline".IX Subsection "Pipeline"The pipeline model divides up a task into a series of steps, andpasses the results of one step on to the thread processing thenext.  Each thread does one thing to each piece of data and passes theresults to the next thread in line..PPThis model makes the most sense if you have multiple processors so twoor more threads will be executing in parallel, though it can oftenmake sense in other contexts as well.  It tends to keep the individualtasks small and simple, as well as allowing some parts of the pipelineto block (on I/O or system calls, for example) while other parts keepgoing.  If you're running different parts of the pipeline on differentprocessors you may also take advantage of the caches on eachprocessor..PPThis model is also handy for a form of recursive programming where,rather than having a subroutine call itself, it instead createsanother thread.  Prime and Fibonacci generators both map well to thisform of the pipeline model. (A version of a prime number generator ispresented later on.).SH "Native threads".IX Header "Native threads"There are several different ways to implement threads on a system.  Howthreads are implemented depends both on the vendor and, in some cases,the version of the operating system.  Often the first implementationwill be relatively simple, but later versions of the \s-1OS\s0 will be moresophisticated..PPWhile the information in this section is useful, it's not necessary,so you can skip it if you don't feel up to it..PPThere are three basic categories of threads-user-mode threads, kernelthreads, and multiprocessor kernel threads..PPUser-mode threads are threads that live entirely within a program andits libraries.  In this model, the \s-1OS\s0 knows nothing about threads.  Asfar as it's concerned, your process is just a process..PPThis is the easiest way to implement threads, and the way most OSesstart.  The big disadvantage is that, since the \s-1OS\s0 knows nothing aboutthreads, if one thread blocks they all do.  Typical blocking activitiesinclude most system calls, most I/O, and things like \fIsleep()\fR..PPKernel threads are the next step in thread evolution.  The \s-1OS\s0 knowsabout kernel threads, and makes allowances for them.  The maindifference between a kernel thread and a user-mode thread isblocking.  With kernel threads, things that block a single thread don'tblock other threads.  This is not the case with user-mode threads,where the kernel blocks at the process level and not the thread level..PPThis is a big step forward, and can give a threaded program quite aperformance boost over non-threaded programs.  Threads that blockperforming I/O, for example, won't block threads that are doing otherthings.  Each process still has only one thread running at once,though, regardless of how many CPUs a system might have..PPSince kernel threading can interrupt a thread at any time, they willuncover some of the implicit locking assumptions you may make in yourprogram.  For example, something as simple as \f(CW\*(C`$a = $a + 2\*(C'\fR can behaveunpredictably with kernel threads if \f(CW$a\fR is visible to otherthreads, as another thread may have changed \f(CW$a\fR between the time itwas fetched on the right hand side and the time the new value isstored..PPMultiprocessor Kernel Threads are the final step in threadsupport.  With multiprocessor kernel threads on a machine with multipleCPUs, the \s-1OS\s0 may schedule two or more threads to run simultaneously ondifferent CPUs..PPThis can give a serious performance boost to your threaded program,since more than one thread will be executing at the same time.  As atradeoff, though, any of those nagging synchronization issues thatmight not have shown with basic kernel threads will appear with avengeance..PPIn addition to the different levels of \s-1OS\s0 involvement in threads,different OSes (and different thread implementations for a particular\&\s-1OS\s0) allocate \s-1CPU\s0 cycles to threads in different ways..PPCooperative multitasking systems have running threads give up controlif one of two things happen.  If a thread calls a yield function, itgives up control.  It also gives up control if the thread doessomething that would cause it to block, such as perform I/O.  In acooperative multitasking implementation, one thread can starve all theothers for \s-1CPU\s0 time if it so chooses..PPPreemptive multitasking systems interrupt threads at regular intervalswhile the system decides which thread should run next.  In a preemptivemultitasking system, one thread usually won't monopolize the \s-1CPU\s0..PPOn some systems, there can be cooperative and preemptive threadsrunning simultaneously. (Threads running with realtime prioritiesoften behave cooperatively, for example, while threads running atnormal priorities behave preemptively.).SH "What kind of threads are perl threads?".IX Header "What kind of threads are perl threads?"If you have experience with other thread implementations, you mightfind that things aren't quite what you expect.  It's very important toremember when dealing with Perl threads that Perl Threads Are Not XThreads, for all values of X.  They aren't \s-1POSIX\s0 threads, or

⌨️ 快捷键说明

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