⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threads.3

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 3
📖 第 1 页 / 共 3 页
字号:
.el .IP "\f(CW$thr2\fR = \f(CW$thr1\fR\->create(\s-1FUNCTION\s0, \s-1ARGS\s0)" 4.IX Item "$thr2 = $thr1->create(FUNCTION, ARGS)"This creates a new thread (\f(CW$thr2\fR) that inherits the stack size from anexisting thread (\f(CW$thr1\fR).  This is shorthand for the following:.Sp.Vb 2\&    my $stack_size = $thr1\->get_stack_size();\&    my $thr2 = threads\->create({\*(Aqstack_size\*(Aq => $stack_size}, FUNCTION, ARGS);.Ve.SH "THREAD SIGNALLING".IX Header "THREAD SIGNALLING"When safe signals is in effect (the default behavior \- see \*(L"Unsafe signals\*(R"for more details), then signals may be sent and acted upon by individualthreads..ie n .IP "$thr\->kill('\s-1SIG\s0...');" 4.el .IP "\f(CW$thr\fR\->kill('\s-1SIG\s0...');" 4.IX Item "$thr->kill('SIG...');"Sends the specified signal to the thread.  Signal names and (positive) signalnumbers are the same as those supported by\&\fIkill()\fR.  For example, '\s-1SIGTERM\s0', '\s-1TERM\s0' and(depending on the \s-1OS\s0) 15 are all valid arguments to \f(CW\*(C`\->kill()\*(C'\fR..SpReturns the thread object to allow for method chaining:.Sp.Vb 1\&    $thr\->kill(\*(AqSIG...\*(Aq)\->join();.Ve.PPSignal handlers need to be set up in the threads for the signals they areexpected to act upon.  Here's an example for \fIcancelling\fR a thread:.PP.Vb 1\&    use threads;\&\&    sub thr_func\&    {\&        # Thread \*(Aqcancellation\*(Aq signal handler\&        $SIG{\*(AqKILL\*(Aq} = sub { threads\->exit(); };\&\&        ...\&    }\&\&    # Create a thread\&    my $thr = threads\->create(\*(Aqthr_func\*(Aq);\&\&    ...\&\&    # Signal the thread to terminate, and then detach\&    # it so that it will get cleaned up automatically\&    $thr\->kill(\*(AqKILL\*(Aq)\->detach();.Ve.PPHere's another simplistic example that illustrates the use of threadsignalling in conjunction with a semaphore to provide rudimentary \fIsuspend\fRand \fIresume\fR capabilities:.PP.Vb 2\&    use threads;\&    use Thread::Semaphore;\&\&    sub thr_func\&    {\&        my $sema = shift;\&\&        # Thread \*(Aqsuspend/resume\*(Aq signal handler\&        $SIG{\*(AqSTOP\*(Aq} = sub {\&            $sema\->down();      # Thread suspended\&            $sema\->up();        # Thread resumes\&        };\&\&        ...\&    }\&\&    # Create a semaphore and pass it to a thread\&    my $sema = Thread::Semaphore\->new();\&    my $thr = threads\->create(\*(Aqthr_func\*(Aq, $sema);\&\&    # Suspend the thread\&    $sema\->down();\&    $thr\->kill(\*(AqSTOP\*(Aq);\&\&    ...\&\&    # Allow the thread to continue\&    $sema\->up();.Ve.PP\&\s-1CAVEAT:\s0  The thread signalling capability provided by this module does notactually send signals via the \s-1OS\s0.  It \fIemulates\fR signals at the Perl-levelsuch that signal handlers are called in the appropriate thread.  For example,sending \f(CW\*(C`$thr\->kill(\*(AqSTOP\*(Aq)\*(C'\fR does not actually suspend a thread (or thewhole process), but does cause a \f(CW$SIG{\*(AqSTOP\*(Aq}\fR handler to be called in thatthread (as illustrated above)..PPAs such, signals that would normally not be appropriate to use in the\&\f(CW\*(C`kill()\*(C'\fR command (e.g., \f(CW\*(C`kill(\*(AqKILL\*(Aq, $$)\*(C'\fR) are okay to use with the\&\f(CW\*(C`\->kill()\*(C'\fR method (again, as illustrated above)..PPCorrespondingly, sending a signal to a thread does not disrupt the operationthe thread is currently working on:  The signal will be acted upon after thecurrent operation has completed.  For instance, if the thread is \fIstuck\fR onan I/O call, sending it a signal will not cause the I/O call to be interruptedsuch that the signal is acted up immediately..PPSending a signal to a terminated thread is ignored..SH "WARNINGS".IX Header "WARNINGS".IP "Perl exited with active threads:" 4.IX Item "Perl exited with active threads:"If the program exits without all threads having either been joined ordetached, then this warning will be issued..Sp\&\s-1NOTE:\s0  If the \fImain\fR thread exits, then this warning cannot be suppressedusing \f(CW\*(C`no warnings \*(Aqthreads\*(Aq;\*(C'\fR as suggested below..IP "Thread creation failed: pthread_create returned #" 4.IX Item "Thread creation failed: pthread_create returned #"See the appropriate \fIman\fR page for \f(CW\*(C`pthread_create\*(C'\fR to determine the actualcause for the failure..IP "Thread # terminated abnormally: ..." 4.IX Item "Thread # terminated abnormally: ..."A thread terminated in some manner other than just returning from its entrypoint function, or by using \f(CW\*(C`threads\->exit()\*(C'\fR.  For example, the threadmay have terminated because of an error, or by using \f(CW\*(C`die\*(C'\fR..IP "Using minimum thread stack size of #" 4.IX Item "Using minimum thread stack size of #"Some platforms have a minimum thread stack size.  Trying to set the stack sizebelow this value will result in the above warning, and the stack size will beset to the minimum..IP "Thread creation failed: pthread_attr_setstacksize(\fI\s-1SIZE\s0\fR) returned 22" 4.IX Item "Thread creation failed: pthread_attr_setstacksize(SIZE) returned 22"The specified \fI\s-1SIZE\s0\fR exceeds the system's maximum stack size.  Use a smallervalue for the stack size..PPIf needed, thread warnings can be suppressed by using:.PP.Vb 1\&    no warnings \*(Aqthreads\*(Aq;.Ve.PPin the appropriate scope..SH "ERRORS".IX Header "ERRORS".IP "This Perl not built to support threads" 4.IX Item "This Perl not built to support threads"The particular copy of Perl that you're trying to use was not built using the\&\f(CW\*(C`useithreads\*(C'\fR configuration option..SpHaving threads support requires all of Perl and all of the \s-1XS\s0 modules in thePerl installation to be rebuilt; it is not just a question of adding thethreads module (i.e., threaded and non-threaded Perls are binaryincompatible.).IP "Cannot change stack size of an existing thread" 4.IX Item "Cannot change stack size of an existing thread"The stack size of currently extant threads cannot be changed, therefore, thefollowing results in the above error:.Sp.Vb 1\&    $thr\->set_stack_size($size);.Ve.IP "Cannot signal threads without safe signals" 4.IX Item "Cannot signal threads without safe signals"Safe signals must be in effect to use the \f(CW\*(C`\->kill()\*(C'\fR signalling method.See \*(L"Unsafe signals\*(R" for more details..IP "Unrecognized signal name: ..." 4.IX Item "Unrecognized signal name: ..."The particular copy of Perl that you're trying to use does not support thespecified signal being used in a \f(CW\*(C`\->kill()\*(C'\fR call..SH "BUGS AND LIMITATIONS".IX Header "BUGS AND LIMITATIONS"Before you consider posting a bug report, please consult, and possibly post amessage to the discussion forum to see if what you've encountered is a knownproblem..IP "Thread-safe modules" 4.IX Item "Thread-safe modules"See \*(L"Making your module threadsafe\*(R" in perlmod when creating modules that maybe used in threaded applications, especially if those modules use non-Perldata, or \s-1XS\s0 code..IP "Using non-thread-safe modules" 4.IX Item "Using non-thread-safe modules"Unfortunately, you may encounter Perl modules that are not \fIthread-safe\fR.For example, they may crash the Perl interpreter during execution, or may dumpcore on termination.  Depending on the module and the requirements of yourapplication, it may be possible to work around such difficulties..SpIf the module will only be used inside a thread, you can try loading themodule from inside the thread entry point function using \f(CW\*(C`require\*(C'\fR (and\&\f(CW\*(C`import\*(C'\fR if needed):.Sp.Vb 4\&    sub thr_func\&    {\&        require Unsafe::Module\&        # Unsafe::Module\->import(...);\&\&        ....\&    }.Ve.SpIf the module is needed inside the \fImain\fR thread, try modifying yourapplication so that the module is loaded (again using \f(CW\*(C`require\*(C'\fR and\&\f(CW\*(C`\->import()\*(C'\fR) after any threads are started, and in such a way that noother threads are started afterwards..SpIf the above does not work, or is not adequate for your application, then filea bug report on <http://rt.cpan.org/Public/> against the problematic module..IP "Current working directory" 4.IX Item "Current working directory"On all platforms except MSWin32, the setting for the current working directoryis shared among all threads such that changing it in one thread (e.g., using\&\f(CW\*(C`chdir()\*(C'\fR) will affect all the threads in the application..SpOn MSWin32, each thread maintains its own the current working directorysetting..IP "Environment variables" 4.IX Item "Environment variables"Currently, on all platforms except MSWin32, all \fIsystem\fR calls (e.g., using\&\f(CW\*(C`system()\*(C'\fR or back-ticks) made from threads use the environment variablesettings from the \fImain\fR thread.  In other words, changes made to \f(CW%ENV\fR ina thread will not be visible in \fIsystem\fR calls made by that thread..SpTo work around this, set environment variables as part of the \fIsystem\fR call.For example:.Sp.Vb 2\&    my $msg = \*(Aqhello\*(Aq;\&    system("FOO=$msg; echo \e$FOO");   # Outputs \*(Aqhello\*(Aq to STDOUT.Ve.SpOn MSWin32, each thread maintains its own set of environment variables..IP "Parent-child threads" 4.IX Item "Parent-child threads"On some platforms, it might not be possible to destroy \fIparent\fR threads whilethere are still existing \fIchild\fR threads..IP "Creating threads inside special blocks" 4.IX Item "Creating threads inside special blocks"Creating threads inside \f(CW\*(C`BEGIN\*(C'\fR, \f(CW\*(C`CHECK\*(C'\fR or \f(CW\*(C`INIT\*(C'\fR blocks should not berelied upon.  Depending on the Perl version and the application code, resultsmay range from success, to (apparently harmless) warnings of leaked scalar, orall the way up to crashing of the Perl interpreter..IP "Unsafe signals" 4.IX Item "Unsafe signals"Since Perl 5.8.0, signals have been made safer in Perl by postponing theirhandling until the interpreter is in a \fIsafe\fR state.  See\&\*(L"Safe Signals\*(R" in perl58delta and \*(L"Deferred Signals (Safe Signals)\*(R" in perlipcfor more details..SpSafe signals is the default behavior, and the old, immediate, unsafesignalling behavior is only in effect in the following situations:.RS 4.IP "\(bu" 4Perl has been built with \f(CW\*(C`PERL_OLD_SIGNALS\*(C'\fR (see \f(CW\*(C`perl \-V\*(C'\fR)..IP "\(bu" 4The environment variable \f(CW\*(C`PERL_SIGNALS\*(C'\fR is set to \f(CW\*(C`unsafe\*(C'\fR (see \*(L"\s-1PERL_SIGNALS\s0\*(R" in perlrun)..IP "\(bu" 4The module Perl::Unsafe::Signals is used..RE.RS 4.SpIf unsafe signals is in effect, then signal handling is not thread-safe, andthe \f(CW\*(C`\->kill()\*(C'\fR signalling method cannot be used..RE.IP "Returning closures from threads" 4.IX Item "Returning closures from threads"Returning closures from threads should not be relied upon.  Depending of thePerl version and the application code, results may range from success, to(apparently harmless) warnings of leaked scalar, or all the way up to crashingof the Perl interpreter..IP "Returning objects from threads" 4.IX Item "Returning objects from threads"Returning objects from threads does not work.  Depending on the classesinvolved, you may be able to work around this by returning a serializedversion of the object (e.g., using Data::Dumper or Storable), and thenreconstituting it in the joining thread..IP "Perl Bugs and the \s-1CPAN\s0 Version of threads" 4.IX Item "Perl Bugs and the CPAN Version of threads"Support for threads extends beyond the code in this module (i.e.,\&\fIthreads.pm\fR and \fIthreads.xs\fR), and into the Perl interpreter itself.  Olderversions of Perl contain bugs that may manifest themselves despite using thelatest version of threads from \s-1CPAN\s0.  There is no workaround for this otherthan upgrading to the latest version of Perl..SpEven with the latest version of Perl, it is known that certain constructswith threads may result in warning messages concerning leaked scalars orunreferenced scalars.  However, such warnings are harmless, and may safely beignored..SH "REQUIREMENTS".IX Header "REQUIREMENTS"Perl 5.8.0 or later.SH "SEE ALSO".IX Header "SEE ALSO"threads Discussion Forum on \s-1CPAN:\s0<http://www.cpanforum.com/dist/threads>.PPAnnotated \s-1POD\s0 for threads:<http://annocpan.org/~JDHEDDEN/threads\-1.67/threads.pm>.PPSource repository:<http://code.google.com/p/threads\-shared/>.PPthreads::shared, perlthrtut.PP<http://www.perl.com/pub/a/2002/06/11/threads.html> and<http://www.perl.com/pub/a/2002/09/04/threads.html>.PPPerl threads mailing list:<http://lists.cpan.org/showlist.cgi?name=iThreads>.PPStack size discussion:<http://www.perlmonks.org/?node_id=532956>.SH "AUTHOR".IX Header "AUTHOR"Artur Bergman <sky \s-1AT\s0 crucially \s-1DOT\s0 net>.PPthreads is released under the same license as Perl..PP\&\s-1CPAN\s0 version produced by Jerry D. Hedden <jdhedden \s-1AT\s0 cpan \s-1DOT\s0 org>.SH "ACKNOWLEDGEMENTS".IX Header "ACKNOWLEDGEMENTS"Richard Soderberg <perl \s-1AT\s0 crystalflame \s-1DOT\s0 net> \-Helping me out tons, trying to find reasons for races and other weird bugs!.PPSimon Cozens <simon \s-1AT\s0 brecon \s-1DOT\s0 co \s-1DOT\s0 uk> \-Being there to answer zillions of annoying questions.PPRocco Caputo <troc \s-1AT\s0 netrus \s-1DOT\s0 net>.PPVipul Ved Prakash <mail \s-1AT\s0 vipul \s-1DOT\s0 net> \-Helping with debugging.PPDean Arnold <darnold \s-1AT\s0 presicient \s-1DOT\s0 com> \-Stack size \s-1API\s0

⌨️ 快捷键说明

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