- 论坛徽章:
- 46
|
threads 的文档说的很清楚啊
EXITING A THREAD
The usual method for terminating a thread is to return EXPR from the entry point function with the appropriate return value(s).
* threads->exit()
If needed, a thread can be exited at any time by calling threads->exit() . This will cause the thread to return undef in a scalar context, or the empty list in a list context.
When called from the main thread, this behaves the same as exit(0).
* threads->exit(status)
When called from a thread, this behaves like threads->exit() (i.e., the exit status code is ignored).
When called from the main thread, this behaves the same as exit(status).
* die()
Calling die() in a thread indicates an abnormal exit for the thread. Any $SIG{__DIE__} handler in the thread will be called first, and then the thread will exit with a warning message that will contain any arguments passed in the die() call.
* exit(status)
Calling exit EXPR inside a thread causes the whole application to terminate. Because of this, the use of exit() inside threaded code, or in modules that might be used in threaded applications, is strongly discouraged.
If exit() really is needed, then consider using the following:
1. threads->exit() if threads->can('exit'); # Thread friendly
2. exit(status);
* use threads 'exit' => 'threads_only'
This globally overrides the default behavior of calling exit() inside a thread, and effectively causes such calls to behave the same as threads->exit() . In other words, with this setting, calling exit() causes only the thread to terminate.
Because of its global effect, this setting should not be used inside modules or the like.
The main thread is unaffected by this setting.
* threads->create({'exit' => 'thread_only'}, ...)
This overrides the default behavior of exit() inside the newly created thread only.
* $thr->set_thread_exit_only(boolean)
This can be used to change the exit thread only behavior for a thread after it has been created. With a true argument, exit() will cause only the thread to exit. With a false argument, exit() will terminate the application.
The main thread is unaffected by this call.
* threads->set_thread_exit_only(boolean)
Class method for use inside a thread to change its own behavior for exit().
The main thread is unaffected by this call. |
|